def init(fips_dir, proj_name): """initialize an existing project directory as a fips directory by copying essential files and creating or updating .gitignore :param fips_dir: absolute path to fips :param proj_name: project directory name (dir must exist) :returns: True if the project was successfully initialized """ ws_dir = util.get_workspace_dir(fips_dir) proj_dir = util.get_project_dir(fips_dir, proj_name) if os.path.isdir(proj_dir): templ_values = {'project': proj_name} for f in ['CMakeLists.txt', 'fips', 'fips.cmd', 'fips.yml']: template.copy_template_file(fips_dir, proj_dir, f, templ_values) os.chmod(proj_dir + '/fips', 0o744) gitignore_entries = ['.fips-*', '*.pyc', '.vscode/', '.idea/'] template.write_git_ignore(proj_dir, gitignore_entries) else: log.error("project dir '{}' does not exist".format(proj_dir)) return False
def init(fips_dir, proj_name) : """initialize an existing project directory as a fips directory by copying essential files and creating or updating .gitignore :param fips_dir: absolute path to fips :param proj_name: project directory name (dir must exist) :returns: True if the project was successfully initialized """ ws_dir = util.get_workspace_dir(fips_dir) proj_dir = util.get_project_dir(fips_dir, proj_name) if os.path.isdir(proj_dir) : templ_values = { 'project': proj_name } for f in ['CMakeLists.txt', 'fips', 'fips.cmd', 'fips.yml'] : template.copy_template_file(fips_dir, proj_dir, f, templ_values) os.chmod(proj_dir + '/fips', 0o744) gitignore_entries = ['.fips-*', '*.pyc'] template.write_git_ignore(proj_dir, gitignore_entries) else : log.error("project dir '{}' does not exist".format(proj_dir)) return False
def write_imports(fips_dir, proj_dir, cfg_name, imported) : """write the big imports map created with 'gather_imports' to a .fips-imports.cmake file in the current project :params fips_dir: absolute path to fips :params proj_dir: absolute path to current project :params imported: the imports dictionary created with 'gather_imports' """ if imported is not None: unique_hdrdirs = [] unique_condhdrdirs = {} unique_libdirs = [] unique_defines = {} unique_modules = {} # write a temporary .fips-imports.cmake.tmp file, # this will replace the old file, but only if the # content is different, this will prevent an unnecessary # cmake run if the imports haven't changed import_filename = proj_dir + '/.fips-imports.cmake' import_tmp_filename = import_filename + '.tmp' with open(import_tmp_filename, 'w') as f : f.write("#\n# generated by 'fips gen', don't edit, don't add to version control!\n#\n") for imp_proj_name in imported : imp_proj_dir = util.get_project_dir(fips_dir, imp_proj_name) if imported[imp_proj_name]['cond']: f.write('if ({})\n'.format(imported[imp_proj_name]['cond'])) # add include and lib search paths if imp_proj_dir != proj_dir : f.write('if (EXISTS "{}/fips-include.cmake")\n'.format(imp_proj_dir)) f.write(' include("{}/fips-include.cmake")\n'.format(imp_proj_dir)) f.write('elseif (EXISTS "{}/fips-files/include.cmake")\n'.format(imp_proj_dir)) f.write(' include ("{}/fips-files/include.cmake")\n'.format(imp_proj_dir)) f.write('endif()\n') f.write('if (EXISTS "{}/lib/${{FIPS_PLATFORM_NAME}}")\n'.format(imp_proj_dir)) f.write(' link_directories("{}/lib/${{FIPS_PLATFORM_NAME}}")\n'.format(imp_proj_dir)) f.write('endif()\n') # add header search paths for hdrdir in imported[imp_proj_name]['hdrdirs'] : if hdrdir not in unique_hdrdirs : f.write('include_directories("{}")\n'.format(hdrdir)) unique_hdrdirs.append(hdrdir) # add conditional header search paths for hdrdir in imported[imp_proj_name]['condhdrdirs'] : value = imported[imp_proj_name]['condhdrdirs'][hdrdir] if hdrdir not in unique_condhdrdirs : unique_condhdrdirs[hdrdir] = value f.write('if ({})\n'.format(value)) f.write(' include_directories("{}")\n'.format(hdrdir)) f.write('endif()\n') # add lib search paths for libdir in imported[imp_proj_name]['libdirs'] : if libdir not in unique_libdirs : f.write('link_directories("{}")\n'.format(libdir)) unique_libdirs.append(libdir) # add defines for define in imported[imp_proj_name]['defines'] : value = imported[imp_proj_name]['defines'][define] if define not in unique_defines : unique_defines[define] = value if type(value) is str : f.write('add_definitions(-D{}="{}")\n'.format(define, value)) else : f.write('add_definitions(-D{}={})\n'.format(define, value)) # add import modules if len(imported[imp_proj_name]['modules']) > 0 : import_functions = [] # first add all module import functions for module in imported[imp_proj_name]['modules'] : module_path = imported[imp_proj_name]['modules'][module] if module not in unique_modules : unique_modules[module] = module_path import_func = 'fips_import_{}'.format(module_path).replace('-','_') import_functions.append(import_func) f.write('macro({})\n'.format(import_func)) f.write(' set(FIPS_IMPORT 1)\n') f.write(' add_subdirectory("{}" "{}")\n'.format(module, module_path)) f.write(' set(FIPS_IMPORT)\n') f.write('endmacro()\n') # if auto-import is enabled, also actually import all modules f.write('if (FIPS_AUTO_IMPORT)\n') group = "Imports" if imported[imp_proj_name]['group'] : group += "/" + imported[imp_proj_name]['group'] if len(imported[imp_proj_name]['modules']) > 3 : group += "/" + imp_proj_name f.write(' fips_ide_group("{}")\n'.format(group)) for import_func in import_functions : f.write(' {}()\n'.format(import_func)) f.write(' fips_ide_group("")\n') f.write('endif()\n') if imported[imp_proj_name]['cond']: f.write('endif()\n') # check content of old and new file, only replace if changed imports_dirty = True if os.path.isfile(import_filename) : if filecmp.cmp(import_filename, import_tmp_filename, shallow=False) : imports_dirty = False if imports_dirty : if os.path.isfile(import_filename) : os.remove(import_filename) os.rename(import_tmp_filename, import_filename) else : os.remove(import_tmp_filename) # write the .fips-imports.py file (copy from template) gen_search_paths = '"{}","{}/generators",\n'.format(fips_dir, fips_dir) proj_gen_dir = util.get_generators_dir(proj_dir) if proj_gen_dir: gen_search_paths += '"{}","{}",\n'.format(proj_dir, proj_gen_dir) for imp_proj_name in imported : gen_dir = util.get_generators_dir(util.get_project_dir(fips_dir, imp_proj_name)) if gen_dir: gen_search_paths += '"' + gen_dir + '",\n' proj_name = util.get_project_name_from_dir(proj_dir) build_dir = util.get_build_dir(fips_dir, proj_name, cfg_name); if not os.path.isdir(build_dir): os.makedirs(build_dir) template.copy_template_file(fips_dir, build_dir, 'fips-gen.py', { 'genpaths': gen_search_paths}, True)
def write_imports(fips_dir, proj_dir, imported) : """write the big imports directory created with 'gather_imports' to a .fips-imports.cmake file in the current project :params fips_dir: absolute path to fips :params proj_dir: absolute path to current project :params imported: the imports dictionary created with 'gather_imports' """ if imported : unique_hdrdirs = [] unique_libdirs = [] unique_defines = {} unique_modules = {} # write a temporary .fips-imports.cmake.tmp file, # this will replace the old file, but only if the # content is different, this will prevent an unnecessary # cmake run if the imports haven't changed import_filename = proj_dir + '/.fips-imports.cmake' import_tmp_filename = import_filename + '.tmp' with open(import_tmp_filename, 'w') as f : f.write("#\n# generated by 'fips gen', don't edit, don't add to version control!\n#\n") for imp_proj_name in imported : imp_proj_dir = util.get_project_dir(fips_dir, imp_proj_name) # add include and lib search paths if imp_proj_dir != proj_dir : f.write('if (EXISTS "{}/fips-include.cmake")\n'.format(imp_proj_dir)) f.write(' include("{}/fips-include.cmake")\n'.format(imp_proj_dir)) f.write('endif()\n') f.write('if (EXISTS "{}/lib/${{FIPS_PLATFORM_NAME}}")\n'.format(imp_proj_dir)) f.write(' link_directories("{}/lib/${{FIPS_PLATFORM_NAME}}")\n'.format(imp_proj_dir)) f.write('endif()\n') # add header search paths for hdrdir in imported[imp_proj_name]['hdrdirs'] : if hdrdir not in unique_hdrdirs : f.write('include_directories("{}")\n'.format(hdrdir)) unique_hdrdirs.append(hdrdir) # add lib search paths for libdir in imported[imp_proj_name]['libdirs'] : if libdir not in unique_libdirs : f.write('link_directories("{}")\n'.format(libdir)) unique_libdirs.append(libdir) # add defines for define in imported[imp_proj_name]['defines'] : value = imported[imp_proj_name]['defines'][define] if define not in unique_defines : unique_defines[define] = value if type(value) is str : f.write('add_definitions(-D{}="{}")\n'.format(define, value)) else : f.write('add_definitions(-D{}={})\n'.format(define, value)) # add modules if len(imported[imp_proj_name]['modules']) > 0 : f.write('fips_ide_group("Imports")\n') for module in imported[imp_proj_name]['modules'] : module_path = imported[imp_proj_name]['modules'][module] if module not in unique_modules : unique_modules[module] = module_path f.write('add_subdirectory("{}" "{}")\n'.format(module, module_path)) f.write('fips_ide_group("")\n') # check content of old and new file, only replace if changed imports_dirty = True if os.path.isfile(import_filename) : if filecmp.cmp(import_filename, import_tmp_filename, shallow=False) : imports_dirty = False if imports_dirty : if os.path.isfile(import_filename) : os.remove(import_filename) os.rename(import_tmp_filename, import_filename) else : os.remove(import_tmp_filename) # write the .fips-imports.py file (copy from template) gen_search_paths = '"{}","{}/generators",\n'.format(fips_dir, fips_dir) if os.path.isdir("{}/fips-generators".format(proj_dir)) : gen_search_paths += '"{}","{}/fips-generators",\n'.format(proj_dir, proj_dir) for imp_proj_name in imported : gen_dir = util.get_project_dir(fips_dir, imp_proj_name) + '/fips-generators' if os.path.isdir(gen_dir) : gen_search_paths += '"' + gen_dir + '",\n' template.copy_template_file(fips_dir, proj_dir, '.fips-gen.py', { 'genpaths': gen_search_paths}, True)
def write_imports(fips_dir, proj_dir, cfg_name, imported) : """write the big imports map created with 'gather_imports' to a .fips-imports.cmake file in the current project :params fips_dir: absolute path to fips :params proj_dir: absolute path to current project :params imported: the imports dictionary created with 'gather_imports' """ if imported : unique_hdrdirs = [] unique_libdirs = [] unique_defines = {} unique_modules = {} # write a temporary .fips-imports.cmake.tmp file, # this will replace the old file, but only if the # content is different, this will prevent an unnecessary # cmake run if the imports haven't changed import_filename = proj_dir + '/.fips-imports.cmake' import_tmp_filename = import_filename + '.tmp' with open(import_tmp_filename, 'w') as f : f.write("#\n# generated by 'fips gen', don't edit, don't add to version control!\n#\n") for imp_proj_name in imported : imp_proj_dir = util.get_project_dir(fips_dir, imp_proj_name) if imported[imp_proj_name]['cond']: f.write('if ({})\n'.format(imported[imp_proj_name]['cond'])) # add include and lib search paths if imp_proj_dir != proj_dir : f.write('if (EXISTS "{}/fips-include.cmake")\n'.format(imp_proj_dir)) f.write(' include("{}/fips-include.cmake")\n'.format(imp_proj_dir)) f.write('elseif (EXISTS "{}/fips-files/include.cmake")\n'.format(imp_proj_dir)) f.write(' include ("{}/fips-files/include.cmake")\n'.format(imp_proj_dir)) f.write('endif()\n') f.write('if (EXISTS "{}/lib/${{FIPS_PLATFORM_NAME}}")\n'.format(imp_proj_dir)) f.write(' link_directories("{}/lib/${{FIPS_PLATFORM_NAME}}")\n'.format(imp_proj_dir)) f.write('endif()\n') # add header search paths for hdrdir in imported[imp_proj_name]['hdrdirs'] : if hdrdir not in unique_hdrdirs : f.write('include_directories("{}")\n'.format(hdrdir)) unique_hdrdirs.append(hdrdir) # add lib search paths for libdir in imported[imp_proj_name]['libdirs'] : if libdir not in unique_libdirs : f.write('link_directories("{}")\n'.format(libdir)) unique_libdirs.append(libdir) # add defines for define in imported[imp_proj_name]['defines'] : value = imported[imp_proj_name]['defines'][define] if define not in unique_defines : unique_defines[define] = value if type(value) is str : f.write('add_definitions(-D{}="{}")\n'.format(define, value)) else : f.write('add_definitions(-D{}={})\n'.format(define, value)) # add import modules if len(imported[imp_proj_name]['modules']) > 0 : import_functions = [] # first add all module import functions for module in imported[imp_proj_name]['modules'] : module_path = imported[imp_proj_name]['modules'][module] if module not in unique_modules : unique_modules[module] = module_path import_func = 'fips_import_{}'.format(module_path).replace('-','_') import_functions.append(import_func) f.write('macro({})\n'.format(import_func)) f.write(' set(FIPS_IMPORT 1)\n') f.write(' add_subdirectory("{}" "{}")\n'.format(module, module_path)) f.write(' set(FIPS_IMPORT)\n') f.write('endmacro()\n') # if auto-import is enabled, also actually import all modules f.write('if (FIPS_AUTO_IMPORT)\n') group = "Imports" if imported[imp_proj_name]['group'] : group += "/" + imported[imp_proj_name]['group'] if len(imported[imp_proj_name]['modules']) > 3 : group += "/" + imp_proj_name f.write(' fips_ide_group("{}")\n'.format(group)) for import_func in import_functions : f.write(' {}()\n'.format(import_func)) f.write(' fips_ide_group("")\n') f.write('endif()\n') if imported[imp_proj_name]['cond']: f.write('endif()\n') # check content of old and new file, only replace if changed imports_dirty = True if os.path.isfile(import_filename) : if filecmp.cmp(import_filename, import_tmp_filename, shallow=False) : imports_dirty = False if imports_dirty : if os.path.isfile(import_filename) : os.remove(import_filename) os.rename(import_tmp_filename, import_filename) else : os.remove(import_tmp_filename) # write the .fips-imports.py file (copy from template) gen_search_paths = '"{}","{}/generators",\n'.format(fips_dir, fips_dir) proj_gen_dir = util.get_generators_dir(proj_dir) if proj_gen_dir: gen_search_paths += '"{}","{}",\n'.format(proj_dir, proj_gen_dir) for imp_proj_name in imported : gen_dir = util.get_generators_dir(util.get_project_dir(fips_dir, imp_proj_name)) if gen_dir: gen_search_paths += '"' + gen_dir + '",\n' proj_name = util.get_project_name_from_dir(proj_dir) build_dir = util.get_build_dir(fips_dir, proj_name, cfg_name); if not os.path.isdir(build_dir): os.makedirs(build_dir) template.copy_template_file(fips_dir, build_dir, 'fips-gen.py', { 'genpaths': gen_search_paths}, True)
def write_imports(fips_dir, proj_dir, imported): """write the big imports directory created with 'gather_imports' to a .fips-imports.cmake file in the current project :params fips_dir: absolute path to fips :params proj_dir: absolute path to current project :params imported: the imports dictionary created with 'gather_imports' """ if imported: unique_hdrdirs = [] unique_libdirs = [] unique_defines = {} unique_modules = {} # write a temporary .fips-imports.cmake.tmp file, # this will replace the old file, but only if the # content is different, this will prevent an unnecessary # cmake run if the imports haven't changed import_filename = proj_dir + '/.fips-imports.cmake' import_tmp_filename = import_filename + '.tmp' with open(import_tmp_filename, 'w') as f: f.write( "#\n# generated by 'fips gen', don't edit, don't add to version control!\n#\n" ) for imp_proj_name in imported: imp_proj_dir = util.get_project_dir(fips_dir, imp_proj_name) # add include and lib search paths if imp_proj_dir != proj_dir: f.write('if (EXISTS "{}/fips-include.cmake")\n'.format( imp_proj_dir)) f.write(' include("{}/fips-include.cmake")\n'.format( imp_proj_dir)) f.write('endif()\n') f.write('if (EXISTS "{}/lib/${{FIPS_PLATFORM_NAME}}")\n'. format(imp_proj_dir)) f.write( ' link_directories("{}/lib/${{FIPS_PLATFORM_NAME}}")\n' .format(imp_proj_dir)) f.write('endif()\n') # add header search paths for hdrdir in imported[imp_proj_name]['hdrdirs']: if hdrdir not in unique_hdrdirs: f.write('include_directories("{}")\n'.format(hdrdir)) unique_hdrdirs.append(hdrdir) # add lib search paths for libdir in imported[imp_proj_name]['libdirs']: if libdir not in unique_libdirs: f.write('link_directories("{}")\n'.format(libdir)) unique_libdirs.append(libdir) # add defines for define in imported[imp_proj_name]['defines']: value = imported[imp_proj_name]['defines'][define] if define not in unique_defines: unique_defines[define] = value if type(value) is str: f.write('add_definitions(-D{}="{}")\n'.format( define, value)) else: f.write('add_definitions(-D{}={})\n'.format( define, value)) # add modules if len(imported[imp_proj_name]['modules']) > 0: f.write('fips_ide_group("Imports")\n') for module in imported[imp_proj_name]['modules']: module_path = imported[imp_proj_name]['modules'][ module] if module not in unique_modules: unique_modules[module] = module_path f.write('add_subdirectory("{}" "{}")\n'.format( module, module_path)) f.write('fips_ide_group("")\n') # check content of old and new file, only replace if changed imports_dirty = True if os.path.isfile(import_filename): if filecmp.cmp(import_filename, import_tmp_filename, shallow=False): imports_dirty = False if imports_dirty: if os.path.isfile(import_filename): os.remove(import_filename) os.rename(import_tmp_filename, import_filename) else: os.remove(import_tmp_filename) # write the .fips-imports.py file (copy from template) gen_search_paths = '"{}","{}/generators",\n'.format(fips_dir, fips_dir) if os.path.isdir("{}/fips-generators".format(proj_dir)): gen_search_paths += '"{}","{}/fips-generators",\n'.format( proj_dir, proj_dir) for imp_proj_name in imported: gen_dir = util.get_project_dir(fips_dir, imp_proj_name) + '/fips-generators' if os.path.isdir(gen_dir): gen_search_paths += '"' + gen_dir + '",\n' template.copy_template_file(fips_dir, proj_dir, '.fips-gen.py', {'genpaths': gen_search_paths}, True)