def __write_compile_flags(context, cmake_file, compiler_flags_key): write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='target_compile_options(${PROJECT_NAME} PRIVATE', end_text=')', property_name=compiler_flags_key, separator=';\n', indent=' ') for file in context.file_contexts: file_cl_var = 'FILE_CL_OPTIONS' text = write_property_of_settings( cmake_file, context.file_contexts[file].settings, context.sln_configurations_map, begin_text='string(CONCAT {0}'.format(file_cl_var), end_text=')', property_name=compiler_flags_key, indent=' ', in_quotes=True) if text: cmake_file.write( ' source_file_compile_options({0} ${{{1}}})\n'.format( file, file_cl_var))
def write_include_directories(context, cmake_file): """ Write include directories of given context to given CMakeLists.txt file :param context: current context data :type context: Context :param cmake_file: CMakeLists.txt IO wrapper :type cmake_file: _io.TextIOWrapper """ has_includes = is_settings_has_data(context.sln_configurations_map, context.settings, 'inc_dirs') if has_includes: write_comment(cmake_file, 'Include directories') include_directories_specifier = 'PUBLIC' if context.private_include_directories: include_directories_specifier = 'PRIVATE' write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='target_include_directories(${{PROJECT_NAME}} {}'. format(include_directories_specifier), end_text=')', property_name='inc_dirs', separator=';\n', in_quotes=True) if has_includes: cmake_file.write('\n')
def __write_link_flags(context, cmake_file, linker_flags_key): write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='target_link_options(${PROJECT_NAME} PRIVATE', end_text=')', property_name=linker_flags_key, separator=';\n', indent=' ')
def write_link_dependencies(context, cmake_file): """ Write link dependencies of project to given cmake file :param context: current context :type context: Context :param cmake_file: CMakeLists.txt IO wrapper :type cmake_file: _io.TextIOWrapper """ if context.target_references: cmake_file.write('# Link with other targets.\n') cmake_file.write('target_link_libraries(${PROJECT_NAME} PUBLIC\n') for reference in context.target_references: cmake_file.write(' {}\n'.format(reference)) msg = 'External library found : {0}'.format(reference) message(context, msg, '') cmake_file.write(')\n\n') if is_settings_has_data(context.sln_configurations_map, context.settings, 'add_lib_deps'): write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='set(ADDITIONAL_LIBRARY_DEPENDENCIES', end_text=')', property_name='add_lib_deps', separator=';\n', in_quotes=True, ) cmake_file.write('target_link_libraries(${PROJECT_NAME} PUBLIC ' '"${ADDITIONAL_LIBRARY_DEPENDENCIES}")\n\n') if is_settings_has_data(context.sln_configurations_map, context.settings, 'target_link_dirs'): write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='target_link_directories(${PROJECT_NAME} PUBLIC', end_text=')', property_name='target_link_dirs', separator=';\n', in_quotes=True) cmake_file.write('\n')
def __write_target_build_events(context, cmake_file, comment, value_name, event_type): has_build_events = is_settings_has_data(context.sln_configurations_map, context.settings, value_name) if has_build_events: write_comment(cmake_file, comment) write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='add_custom_command_if(\n' ' TARGET ${{PROJECT_NAME}}\n' ' {0}\n' ' COMMANDS'.format(event_type), end_text=')', property_name=value_name, write_setting_property_func=Dependencies. write_target_build_event_of_setting) cmake_file.write('\n')
def write_target_property_sheets(context, cmake_file): """ Write target property sheets of current context :param context: current context :type context: Context :param cmake_file: CMakeLists.txt IO wrapper :type cmake_file: _io.TextIOWrapper """ if is_settings_has_data(context.sln_configurations_map, context.settings, 'property_sheets'): write_comment(cmake_file, 'Includes for CMake from *.props') write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='', end_text='', property_name='property_sheets', write_setting_property_func=Dependencies.write_property_sheets) cmake_file.write('\n')
def write_defines(self, context, cmake_file): """ Routine that writes compile definitions into CMake file """ write_comment(cmake_file, 'Compile definitions') write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='target_compile_definitions(${PROJECT_NAME} PRIVATE', end_text=')', property_name=defines, separator=';\n', in_quotes=True) for file in context.file_contexts: write_property_of_settings( cmake_file, context.file_contexts[file].settings, context.sln_configurations_map, begin_text='set_source_files_properties({0} PROPERTIES'.format( file), end_text=')', property_name=defines, write_setting_property_func=self.__write_defines_for_files) cmake_file.write('\n')
def __write_file_custom_build_events(context, cmake_file, comment, value_name, event_type): del event_type has_build_events = False for file in context.file_contexts: file_context = context.file_contexts[file] has_build_events = is_settings_has_data( context.sln_configurations_map, file_context.settings, value_name) if has_build_events: break if has_build_events: write_comment(cmake_file, comment) for file in context.file_contexts: file_context = context.file_contexts[file] outputs = '' description = '' for setting in file_context.settings: file_setting = file_context.settings[setting] if 'file_custom_build_events' in file_setting: outputs = file_setting['file_custom_build_events'][ 'outputs'] description = file_setting['file_custom_build_events'][ 'description'] text = write_property_of_settings( cmake_file, file_context.settings, context.sln_configurations_map, begin_text='add_custom_command_if(\n' ' OUTPUT "{0}"\n' ' COMMANDS'.format(outputs), end_text=' DEPENDS "{0}"\n' ' COMMENT "{1}"\n)'.format(file, description), property_name='file_custom_build_events', write_setting_property_func=Dependencies. write_file_build_event_of_setting) if text: cmake_file.write('\n')
def write_target_dependency_packages(context, cmake_file): """ Write target dependency packages of current context :param context: current context :type context: Context :param cmake_file: CMakeLists.txt IO wrapper :type cmake_file: _io.TextIOWrapper """ for package in context.packages: for package_property in package[2]: id_version = '{0}.{1}'.format(package[0], package[1]) for setting in context.settings: if 'packages' not in context.settings[setting]: continue if id_version not in context.settings[setting]['packages']: continue if package_property not in context.settings[setting][ 'packages'][id_version]: continue context.settings[setting][id_version + package_property] =\ context.settings[setting]['packages'][id_version][package_property] package_property_variable = package_property + '_VAR' has_written = write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='string(CONCAT "{0}"'.format( package_property_variable), end_text=')', property_name=id_version + package_property, ) if has_written: cmake_file.write( 'set_target_properties(${{PROJECT_NAME}} PROPERTIES "{0}" ${{{1}}})\n' .format(package_property, package_property_variable)) cmake_file.write('use_package(${{PROJECT_NAME}} {0} {1})\n'.format( package[0], package[1]))
def write_target_outputs(context, cmake_file): """ Add outputs for each artefacts CMake target :param context: related full context :type context: Context :param cmake_file: CMakeLists.txt IO wrapper :type cmake_file: _io.TextIOWrapper """ if not context.settings: return if context.root_namespace: cmake_file.write('set(ROOT_NAMESPACE {})\n\n'.format( context.root_namespace)) write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='set_target_properties(${PROJECT_NAME} PROPERTIES', end_text=')', property_name='VS_GLOBAL_KEYWORD', write_setting_property_func=ProjectVariables.write_target_property) if is_settings_has_data(context.sln_configurations_map, context.settings, 'TARGET_NAME'): write_comment(cmake_file, 'Target name') write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='set_target_properties(${PROJECT_NAME} PROPERTIES', end_text=')', property_name='TARGET_NAME', write_setting_property_func=ProjectVariables. write_target_property) if is_settings_has_data(context.sln_configurations_map, context.settings, 'OUTPUT_DIRECTORY'): write_comment(cmake_file, 'Output directory') write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='set_target_properties(${PROJECT_NAME} PROPERTIES', end_text=')', property_name='OUTPUT_DIRECTORY', write_setting_property_func=ProjectVariables. write_target_property) write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='set_target_properties(${PROJECT_NAME} PROPERTIES', end_text=')', property_name='ARCHIVE_OUTPUT_DIRECTORY', write_setting_property_func=ProjectVariables.write_target_property) write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='set_target_properties(${PROJECT_NAME} PROPERTIES', end_text=')', property_name='ARCHIVE_OUTPUT_NAME', write_setting_property_func=ProjectVariables.write_target_property) write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='set_target_properties(${PROJECT_NAME} PROPERTIES', end_text=')', property_name='PDB_OUTPUT_DIRECTORY', write_setting_property_func=ProjectVariables.write_target_property) # PDB_NAME doesn't support generator expressions yet (CMake 3.13) # write_property_of_settings( # cmake_file, context.settings, # context.sln_configurations_map, # begin_text='set_target_properties(${PROJECT_NAME} PROPERTIES', # end_text=')', # property_name='PDB_NAME', # write_setting_property_func=ProjectVariables.write_target_property # ) write_property_of_settings( cmake_file, context.settings, context.sln_configurations_map, begin_text='set_target_properties(${PROJECT_NAME} PROPERTIES', end_text=')', property_name='INTERPROCEDURAL_OPTIMIZATION', write_setting_property_func=ProjectVariables.write_target_property)