Skip to content

Commit

Permalink
Create cmake lists file with generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Jun 17, 2021
1 parent bcdf30a commit 3219f55
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
14 changes: 7 additions & 7 deletions cmake/podioMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,27 @@ function(PODIO_GENERATE_DATAMODEL datamodel YAML_FILE RETURN_HEADERS RETURN_SOUR
# At least build the ROOT selection.xml by default for now
SET(ARG_IO_BACKEND_HANDLERS "ROOT")
ENDIF()

# we need to boostrap the data model, so this has to be executed in the cmake run
execute_process(
COMMAND ${CMAKE_COMMAND} -E echo "Creating \"${datamodel}\" data model"
COMMAND python ${podio_PYTHON_DIR}/podio_class_generator.py ${YAML_FILE} ${ARG_OUTPUT_FOLDER} ${datamodel} ${ARG_IO_BACKEND_HANDLERS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

file(GLOB headers ${ARG_OUTPUT_FOLDER}/${datamodel}/*.h)
file(GLOB sources ${ARG_OUTPUT_FOLDER}/src/*.cc)

set (${RETURN_HEADERS} ${headers} PARENT_SCOPE)
set (${RETURN_SOURCES} ${sources} PARENT_SCOPE)

add_custom_target(create_${datamodel}
COMMENT "Re-Creating \"${datamodel}\" data model"
DEPENDS ${YAML_FILE}
BYPRODUCTS ${sources} ${headers}
BYPRODUCTS ${ARG_OUTPUT_FOLDER}/podio_generated_files.cmake
COMMAND python ${podio_PYTHON_DIR}/podio_class_generator.py --quiet ${YAML_FILE} ${ARG_OUTPUT_FOLDER} ${datamodel} ${ARG_IO_BACKEND_HANDLERS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

include(${ARG_OUTPUT_FOLDER}/podio_generated_files.cmake)

set (${RETURN_HEADERS} ${headers} PARENT_SCOPE)
set (${RETURN_SOURCES} ${sources} PARENT_SCOPE)

endfunction()


Expand Down
34 changes: 34 additions & 0 deletions python/podio_class_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(self, yamlfile, install_dir, package_name, io_handlers, verbose, dr
self.expose_pod_members = self.reader.options["exposePODMembers"]

self.clang_format = []
self.generated_files = []

def process(self):
for name, component in self.reader.components.items():
Expand All @@ -90,6 +91,8 @@ def process(self):
self._create_selection_xml()
self.print_report()

self._write_cmake_lists_file()

def print_report(self):
if not self.verbose:
return
Expand Down Expand Up @@ -120,6 +123,7 @@ def _write_file(self, name, content):
else:
fullname = os.path.join(self.install_dir, "src", name)
if not self.dryrun:
self.generated_files.append(fullname)
if self.clang_format:
cfproc = subprocess.Popen(self.clang_format, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
content = cfproc.communicate(input=content.encode())[0].decode()
Expand Down Expand Up @@ -372,6 +376,36 @@ def _get_member_includes(self, members):

return self._sort_includes(includes)

def _write_cmake_lists_file(self):
"""Write the names of all generated header and src files into cmake lists"""
header_files = (f for f in self.generated_files if f.endswith('.h'))
src_files = (f for f in self.generated_files if f.endswith('.cc'))
xml_files = (f for f in self.generated_files if f.endswith('.xml'))

def _write_list(list_file, name, target_folder, files, comment):
"""Write all files into a cmake variable using the target_folder as path to the
file"""
list_file.write(f'# {comment}\n')
list_file.write(f'SET({name}\n')
for full_file in files:
fname = os.path.basename(full_file)
list_file.write(f' {os.path.join(target_folder, fname)}\n')

list_file.write(')\n')

with open(f'{self.install_dir}/podio_generated_files.cmake', 'w') as list_file:
list_file.write('# AUTOMATICALLY GENERATED FILE - DO NOT EDIT\n\n')

_write_list(list_file, 'headers', r'${ARG_OUTPUT_FOLDER}/${datamodel}',
header_files, 'Generated header files')

_write_list(list_file, 'sources', r'${ARG_OUTPUT_FOLDER}/src',
src_files, 'Generated source files')

_write_list(list_file, 'selection_xml', r'${ARG_OUTPUT_FOLDER}/src',
xml_files, 'Generated xml files')


@staticmethod
def _is_pod_type(members):
"""Check if the members of the class define a POD type"""
Expand Down

0 comments on commit 3219f55

Please sign in to comment.