def create_data(self): """ Create the data and convert each part of "vcxproj" project """ # Write variables variables = ProjectVariables(self.data) variables.add_project_variables() files = ProjectFiles(self.data) files.collects_source_files() variables.add_cmake_project(files.language) variables.add_default_target() # Write Output Variables variables.add_cmake_output_directories() variables.write_project_messages() # Add includes files & directories if self.data['includecmake'] or self.data['include']: title = get_title('Includes', 'Include files and directories') self.data['cmake'].write(title) # Include ".cmake" file if self.data['includecmake']: files.add_include_cmake(self.data['includecmake']) # Write Include Directories depends = Dependencies(self.data) if self.data['include']: depends.write_include_dir() else: message('Include Directories is not set.', '') # Write Dependencies depends.write_dependencies() # Add additional code if self.data['additional'] is not None: files.add_additional_code(self.data['additional']) # Write and add Files files.write_source_files() if files.sources: files.add_target_artefact() # Write Flags all_flags = Flags(self.data) all_flags.write_flags() # Write Macro all_flags.write_defines_and_flags() # Link with other dependencies title = get_title('Link & Dependencies', 'Add project dependencies and Link to project') self.data['cmake'].write(title) depends.add_dependencies() depends.link_dependencies()
def add_additional_code(self, filename): """ Add additional file with CMake code inside :param filename: the file who contains CMake code :type filename: str """ if filename: try: fc = open(filename) title = get_title('Additional Code', 'Provides from an external file') self.cmake.write(title) for line in fc: self.cmake.write(line) fc.close() self.cmake.write('\n\n') message('File of Code is added = ' + filename, 'warn') except OSError as e: message(str(e), 'error') message( 'Wrong data file ! Code was not added, please check given file !', 'error')
def add_cmake_output_directories(self): """ Add output directory for each artefacts CMake target """ title = get_title('Artefacts Output', 'Defines outputs , depending BUILD TYPE') self.cmake.write(title) self.cmake.write('if(CMAKE_BUILD_TYPE STREQUAL "Debug")\n') self.cmake.write( ' set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_DEBUG}")\n' ) self.cmake.write( ' set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_DEBUG}")\n' ) self.cmake.write( ' set(CMAKE_EXECUTABLE_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_DEBUG}")' '\n') self.cmake.write('else()\n') self.cmake.write( ' set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_RELEASE}")\n' ) self.cmake.write( ' set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_RELEASE}")\n' ) self.cmake.write( ' set(CMAKE_EXECUTABLE_OUTPUT_DIRECTORY "${PROJECT_DIR}/${OUTPUT_RELEASE}")\n' ) self.cmake.write('endif()\n\n')
def test_get_title(self): """Get Title""" under_test = get_title('Title', 'text of title') self.assertEqual( '######################### Title ############################\n' '# text of title #\n' '############################################################\n\n', under_test)
def write_dependencies(self): """ Write on "CMakeLists.txt" subdirectories or link directories for external libraries. """ references = self.tree.xpath('//ns:ProjectReference', namespaces=self.ns) if references: title = get_title('Dependencies', 'Add Dependencies to project') self.cmake.write(title) self.cmake.write( '# Dependencies: disable BUILD_DEPENDS to link with libraries already built\n' ) self.cmake.write( 'option(BUILD_DEPENDS "Build other CMake project." ON)\n\n') if not self.custom_dependencies: self.cmake.write('if(BUILD_DEPENDS)\n') for ref in references: reference = str(ref.get('Include')) path_to_reference = os.path.splitext( ntpath.basename(reference))[0] if 'g3log' in path_to_reference: path_to_reference = '%sger' % path_to_reference self.cmake.write(' if(NOT TARGET %s)\n' % path_to_reference) self.cmake.write( ' add_subdirectory("${%s_DIR}" ${CMAKE_BINARY_DIR}/%s)\n' % (path_to_reference.upper(), path_to_reference)) self.cmake.write(' endif()\n') else: self.cmake.write('if(BUILD_DEPENDS)\n') d = 1 for ref in self.custom_dependencies: self.cmake.write( ' add_subdirectory(%s ${CMAKE_BINARY_DIR}/lib%s)\n' % (ref, str(d))) message( 'Add manually dependencies : %s. Will be build in "lib%s/" !' % (ref, str(d)), 'warn') d += 1 self.cmake.write('else()\n') for ref in references: reference = str(ref.get('Include')) path_to_reference = os.path.splitext( ntpath.basename(reference))[0] self.cmake.write( ' link_directories(${DEPENDENCIES_DIR}/%s)\n' % path_to_reference) self.cmake.write('endif()\n\n') else: # pragma: no cover message('No link needed.', '')
def write_flags(self): """ Parse all flags properties and write them inside "CMakeLists.txt" file """ title = get_title('Flags', 'Defines Flags for Windows and Linux') self.cmake.write(title) self.define_group_properties() self.define_windows_flags() self.define_defines() self.define_linux_flags()
def add_cmake_project(self, language): """ Add CMake Project :param language: type of project language: cpp | c :type language: list """ cpp_extensions = ['cc', 'cp', 'cxx', 'cpp', 'CPP', 'c++', 'C'] available_language = {'c': 'C'} available_language.update(dict.fromkeys(cpp_extensions, 'CXX')) self.cmake.write('\n') title = get_title('CMake Project', 'The main options of project') self.cmake.write(title) lang = 'cpp' if len(language) is not 0: lang = language[0] self.cmake.write('project(${PROJECT_NAME} %s)\n\n' % available_language[lang].upper())
def add_project_variables(self): # pylint: disable=too-many-locals """ Add main CMake project variables """ # Project Name title = get_title('Variables', 'Change if you want modify path or other values') self.cmake.write(title) root_projectname = self.tree.xpath('//ns:RootNamespace', namespaces=self.ns) project = False self.cmake.write('# Project\n') self.cmake.write( 'get_filename_component(PROJECT_DIR "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE)\n' ) self.cmake.write('set(DEPENDENCIES_DIR ${PROJECT_DIR}/dependencies)\n') if root_projectname: projectname = root_projectname[0] if projectname.text: if 'g3log' in projectname.text: projectname.text = '%sger' % projectname.text self.cmake.write('set(PROJECT_NAME ' + projectname.text + ')\n\n') project = True if not project: # pragma: no cover self.cmake.write( 'set(PROJECT_NAME <PLEASE SET YOUR PROJECT NAME !!>)\n\n') message( 'No PROJECT NAME found or define. ' 'Please set [PROJECT_NAME] variable in CMakeLists.txt.', 'error') self.add_dependencies_variables() self.add_output_variables()
def write_source_files(self): """ Write source files variables to file() cmake function """ def add_specific_sources(src_list, condition): # pragma: no cover """ Add specific sources for platforms :param src_list: list of source file :type src_list: list :param condition: condition to add in "if" statement :type condition: str """ self.cmake.write('if(%sMSVC)\n' % condition) self.cmake.write(' set(SRC_FILES\n ${SRC_FILES}\n') while src_list: self.cmake.write(' %s\n' % src_list.pop()) self.cmake.write(' )\n') self.cmake.write('endif()\n') windows_sources = [] linux_sources = [] title = get_title('Files & Targets', 'Files of project and target to build') self.cmake.write(title) self.cmake.write('# Source Files\n') self.cmake.write('set(SRC_FILES\n') for src_dir in self.sources: for src_file in self.sources[src_dir]: if src_file in [ 'crashhandler_windows.cpp', 'stacktrace_windows.cpp' ]: windows_sources.append(os.path.join(src_dir, src_file)) if 'crashhandler_windows.cpp' in src_file: linux_sources.append( os.path.join(src_dir, src_file.replace('windows', 'unix'))) else: self.cmake.write(' %s\n' % os.path.join(src_dir, src_file)) self.cmake.write(')\n') # Manage specific sources (Currently only used for g3log) if windows_sources: add_specific_sources(windows_sources, '') if linux_sources: add_specific_sources(linux_sources, 'NOT ') self.cmake.write('source_group("Sources" FILES ${SRC_FILES})\n\n') self.cmake.write('# Header Files\n') self.cmake.write('set(HEADERS_FILES\n') for hdrs_dir in self.headers: for header_file in self.headers[hdrs_dir]: self.cmake.write(' %s\n' % os.path.join(hdrs_dir, header_file)) self.cmake.write(')\n') self.cmake.write('source_group("Headers" FILES ${HEADERS_FILES})\n\n')