diff --git a/silecs-codegen/src/xml/fesa/fesa_3_0_0/fesaTemplates.py b/silecs-codegen/src/xml/fesa/fesa_3_0_0/fesaTemplates.py index e62a4801e736183353811925238570213f5b53f7..2bd5dd04c2eba9ec0e7bf0c7e18dec240035c3c0 100644 --- a/silecs-codegen/src/xml/fesa/fesa_3_0_0/fesaTemplates.py +++ b/silecs-codegen/src/xml/fesa/fesa_3_0_0/fesaTemplates.py @@ -628,34 +628,27 @@ cSetArray2DRegData = """ } """ -makeDesign = """# Include SILECS library path -SILECS_PATH ?= ${centralMakefilePath} +makeDesign = """# Include SILECS makefile +include Makefile.silecs # Additional compiler warning flags override WARNFLAGS += # Additional compiler flags -COMPILER_FLAGS += -I$(SILECS_PATH)/include COMPILER_FLAGS += LINKER_FLAGS += # Additional headers (Custom.h Specific.h ...) which need to be installed EXTRA_HEADERS += """ -makeDeploy = """# Include SILECS library path -SILECS_PATH ?= ${silecsBasePath} - -SNAP7_BASE = ${snap7BasePath} +makeDeploy = """# Include SILECS makefile +include Makefile.silecs # Additional compiler warning flags override WARNFLAGS += # Additional libs and flags which are common to the Realtime and Server part COMPILER_FLAGS += -LINKER_FLAGS += -L$(SILECS_PATH)/lib/$(CPU) -lsilecs-comm -LINKER_FLAGS += -L$(SNAP7_BASE)/bin/$(CPU)-linux -lsnap7 -#add default search path for dynamic snap7 library -LINKER_FLAGS += -Wl,-rpath,$(SNAP7_BASE)/bin/$(CPU)-linux,-rpath,/usr/lib LINKER_FLAGS += # Additional libs and flags which are specific to the Realtime part @@ -833,8 +826,8 @@ def genCSetArray2DRegData(register): context = "" return cSetArray2DRegData.replace('${regName}', register.name).replace('${fesaFieldName_upper}', register.getFesaFieldNameCapitalized()).replace('${regType}', register.getSilecsTypeCapitalized()).replace('${fesaFieldName}', register.getFesaFieldName()).replace('${context}', context) -def genMakeDesign(centralMakefilePath): - return makeDesign.replace('${centralMakefilePath}', centralMakefilePath) +def genMakeDesign(): + return makeDesign -def genMakeDeploy(silecsBasePath,snap7BasePath): - return makeDeploy.replace('${silecsBasePath}', silecsBasePath).replace('${snap7BasePath}', snap7BasePath) +def genMakeDeploy(): + return makeDeploy diff --git a/silecs-codegen/src/xml/fesa/fesa_3_0_0/generateBuildEnvironment.py b/silecs-codegen/src/xml/fesa/fesa_3_0_0/generateBuildEnvironment.py index b6e6ed56fc78fa0e828e4f36a31d5a551c1274a9..e2022845590df6650f9f5f0b517c1b1b3445a200 100644 --- a/silecs-codegen/src/xml/fesa/fesa_3_0_0/generateBuildEnvironment.py +++ b/silecs-codegen/src/xml/fesa/fesa_3_0_0/generateBuildEnvironment.py @@ -33,9 +33,9 @@ from iecommon import * # Generates two Makefile.specific, one for the class design # and another for the deploy unit #------------------------------------------------------------------------- -def genMakefileClass(projectPath, centralMakefilePath, logTopics={'errorlog': True} ): +def genMakefileClass(projectPath, logTopics={'errorlog': True} ): # Generate makefile for class design - source = fesaTemplates.genMakeDesign(centralMakefilePath) + source = fesaTemplates.genMakeDesign() makefile = projectPath + "/" + "Makefile.specific" if os.path.isfile(makefile): #dont overwrite return @@ -44,9 +44,9 @@ def genMakefileClass(projectPath, centralMakefilePath, logTopics={'errorlog': Tr fdesc.write(source) fdesc.close() -def genMakefileDU(projectPath, silecsBasePath, snap7BasePath, logTopics={'errorlog': True}): +def genMakefileDU(projectPath, logTopics={'errorlog': True}): # Generate makefile for class design - source = fesaTemplates.genMakeDeploy(silecsBasePath,snap7BasePath) + source = fesaTemplates.genMakeDeploy() # Write file and save makefile = projectPath + "/" + "Makefile.specific" if os.path.isfile(makefile): #dont overwrite diff --git a/silecs-codegen/src/xml/fesa/fesa_3_1_0/generateBuildEnvironment.py b/silecs-codegen/src/xml/fesa/fesa_3_1_0/generateBuildEnvironment.py index b218687508da8c94669f8394caa208205405dc17..92e514a61d3fb5cde2748bde72e0f7e81c78bab2 100644 --- a/silecs-codegen/src/xml/fesa/fesa_3_1_0/generateBuildEnvironment.py +++ b/silecs-codegen/src/xml/fesa/fesa_3_1_0/generateBuildEnvironment.py @@ -16,11 +16,11 @@ import fesa.fesa_3_0_0.generateBuildEnvironment -def genMakefileClass(projectPath, centralMakefilePath, logTopics={'errorlog': True} ): - return fesa.fesa_3_0_0.generateBuildEnvironment.genMakefileClass(projectPath, centralMakefilePath, logTopics ) +def genMakefileClass(projectPath, logTopics={'errorlog': True} ): + return fesa.fesa_3_0_0.generateBuildEnvironment.genMakefileClass(projectPath, logTopics ) -def genMakefileDU(projectPath, silecsBasePath, snap7BasePath, logTopics={'errorlog': True}): - return fesa.fesa_3_0_0.generateBuildEnvironment.genMakefileDU(projectPath, silecsBasePath, snap7BasePath, logTopics ) +def genMakefileDU(projectPath, logTopics={'errorlog': True}): + return fesa.fesa_3_0_0.generateBuildEnvironment.genMakefileDU(projectPath, logTopics ) def genCProjectFile(projectPath, logTopics={'errorlog': True}): return fesa.fesa_3_0_0.generateBuildEnvironment.genCProjectFile(projectPath, logTopics ) diff --git a/silecs-codegen/src/xml/fesa/fesa_7_3_0/generateBuildEnvironment.py b/silecs-codegen/src/xml/fesa/fesa_7_3_0/generateBuildEnvironment.py index c67268416700d34c231863a8180e2c988b0a0598..edb898b044872eda42cfa822b90d3e11a08a99ff 100644 --- a/silecs-codegen/src/xml/fesa/fesa_7_3_0/generateBuildEnvironment.py +++ b/silecs-codegen/src/xml/fesa/fesa_7_3_0/generateBuildEnvironment.py @@ -16,11 +16,11 @@ import fesa.fesa_3_1_0.generateBuildEnvironment -def genMakefileClass(projectPath, centralMakefilePath, logTopics={'errorlog': True} ): - return fesa.fesa_3_1_0.generateBuildEnvironment.genMakefileClass(projectPath, centralMakefilePath, logTopics ) +def genMakefileClass(projectPath, logTopics={'errorlog': True} ): + return fesa.fesa_3_1_0.generateBuildEnvironment.genMakefileClass(projectPath, logTopics ) -def genMakefileDU(projectPath, silecsBasePath, snap7BasePath, logTopics={'errorlog': True}): - return fesa.fesa_3_1_0.generateBuildEnvironment.genMakefileDU(projectPath, silecsBasePath, snap7BasePath, logTopics ) +def genMakefileDU(projectPath, logTopics={'errorlog': True}): + return fesa.fesa_3_1_0.generateBuildEnvironment.genMakefileDU(projectPath, logTopics ) def genCProjectFile(projectPath, logTopics={'errorlog': True}): return fesa.fesa_3_1_0.generateBuildEnvironment.genCProjectFile(projectPath, logTopics ) diff --git a/silecs-codegen/src/xml/migration/FileUtils.py b/silecs-codegen/src/xml/migration/FileUtils.py index 664fc7bcd90c092bd976c316128ce81674250a63..fce0e15da1c3c4c407259da3f94dc3b3bb9ca527 100644 --- a/silecs-codegen/src/xml/migration/FileUtils.py +++ b/silecs-codegen/src/xml/migration/FileUtils.py @@ -53,10 +53,3 @@ def getFesaSourceFiles(fesaClassName, projectDir): print(os.path.join(root,file)) sourceFiles.append(os.path.join(root,file)) return sourceFiles - -def replaceInFile(filePath,searchString,replaceWithString): - with open(filePath, 'r') as file : - filedata = file.read() - filedata = filedata.replace(searchString, replaceWithString) - with open(filePath, 'w') as file: - file.write(filedata) \ No newline at end of file diff --git a/silecs-codegen/src/xml/migration/migrationBase.py b/silecs-codegen/src/xml/migration/migrationBase.py index 050202b5ddb5b581dcdf6af43da2da87dd4108fd..95a27fd3c460eab8cd113927489d7f049a21b774 100644 --- a/silecs-codegen/src/xml/migration/migrationBase.py +++ b/silecs-codegen/src/xml/migration/migrationBase.py @@ -45,18 +45,33 @@ class MigrationBase(object): projectDir = FileUtils.getProjectDir(self.silecsDocument) makeSpecific = os.path.join(projectDir,"Makefile.specific") FileUtils.backupFile(makeSpecific) - + def updateFESAMakeSpecific(self): projectDir = FileUtils.getProjectDir(self.silecsDocument) makeSpecific = os.path.join(projectDir,"Makefile.specific") - print("Migration-Info: Old Version-Strings in '" + makeSpecific + "' will be replaced\n") - if os.path.isfile(makeSpecific): - oldComm = "silecs-communication-cpp/" + self.versionOld - newComm = "silecs-communication-cpp/" + self.versionNew - FileUtils.replaceInFile(makeSpecific,oldComm,newComm) - oldSnap7 = "snap7/" + self.versionOld - newSnap7 = "snap7/" + self.versionNew - FileUtils.replaceInFile(makeSpecific,oldSnap7,newSnap7) + print(f"Migration-Info: {makeSpecific} will be updated.\n") + if FileUtils.fileExists(makeSpecific): + included = False + with open(makeSpecific, 'r') as file: + filedata = file.read() + if "include Makefile.silecs" in filedata: + # Correct include already in file, we have nothing to do. + included = True + + if not included: + lines = [] + for line in filedata.split('\n'): + if ("SILECS" in line) or ("snap7" in line) or ("SNAP7" in line): + print(f"Removing line '{line}'") + else: + lines.append(line + '\n') + + lines.insert(0, "include Makefile.silecs\n") + lines.insert(0, "# Include SILECS makefile\n") + + with open(makeSpecific, 'w') as file: + for line in lines: + file.write(line) def removeGenCode(self): projectDir = FileUtils.getProjectDir(self.silecsDocument) @@ -99,6 +114,7 @@ class MigrationBase(object): # only chaning the version or the schema does not count as modification --> no backup needed self.fixSilecsVersion(context) self.fixXMLScheman(context,self.xmlSchema) + self.updateFESAMakeSpecific() if modified and self.createBackup: self._saveAndBackupFile(context, self.silecsDocument) diff --git a/silecs_cli/silecs.py b/silecs_cli/silecs.py index ba1b77b851904995ffad81038dcbeaf3edc4f99d..cd1ff67dc3d98ee524e2129b29f8b635da41bf0c 100644 --- a/silecs_cli/silecs.py +++ b/silecs_cli/silecs.py @@ -190,9 +190,31 @@ class SilecsCli: except Exception as e: print(e) - ##### Generation source code for silecs deploy path ###### + def update_makefile(self, project_path): + makeSpecific = os.path.join(project_path, "Makefile.specific") + if os.path.isfile(makeSpecific): + silecs_utils.create_backup_file(makeSpecific) + + included = False + with open(makeSpecific, 'r') as file: + filedata = file.read() + if "include Makefile.silecs" in filedata: + # Correct include already in file, we have nothing to do. + included = True + + if not included: + filedata = "# Include SILECS makefile\n" + "include Makefile.silecs\n" + filedata + + with open(makeSpecific, 'w') as file: + file.write(filedata) + else: + # In case the Makefile.specific does not exist. Print an info message. + info = "Please note that you need to include the Makefile.silecs in your " \ + "Makefile.specific by adding the line 'include Makefile.silecs'.\n" + print(info) + def generate_silecsdeploy(self, silecs_deploy_path): """Generate source files from silecs deploy file""" project_name = silecs_utils.get_project_name(silecs_deploy_path) @@ -227,9 +249,8 @@ class SilecsCli: genduwrapper.genDuWrapper(workspace_path, project_name, SILECS_DEPLOY_VERSION) fillFESADeployUnit.fillDeployUnit(workspace_path, project_name, self.silecs_env.FESA_XSD, self.silecs_env.FESA_VERSION) - info = "Please note that you need to add the dependency to the silecs library to your Makefile. \n" \ - "Here an example: https://www-acc.gsi.de/wiki/Frontend/Silecs_1_0_0_CodeSnippets#Dependency_in_Makefile.specific !\n" - print(info) + self.update_makefile(project_path) + print("Code generation finished") @@ -260,9 +281,7 @@ class SilecsCli: print(info) silecs_utils.create_backup_file(fesa_design_file) - info = "Please not that you need to add the dependency to the silecs library to your Makefile. \n" \ - "Here an example: https://www-acc.gsi.de/wiki/Frontend/Silecs_1_0_0_CodeSnippets#Dependency_in_Makefile.specific !\n" - print(info) + self.update_makefile(project_path) generateFesaDesign.fillDesignFile(self.silecs_env.FESA_VERSION, project_name, workspace_path, self.silecs_env.FESA_XSD) generateSourceCode.genCppFiles(project_name, workspace_path, silecs_design_path) @@ -273,14 +292,22 @@ class SilecsCli: """Generate source files""" extension = silecs_utils.get_extension(silecs_design_deploy_path) silecs_design_deploy_path = os.path.abspath(silecs_design_deploy_path) + makefile_path = os.path.join(silecs_utils.get_project_path(silecs_design_deploy_path), silecs_const.SILECS_MAKEFILE) silecs_version = silecs_utils.get_silecs_version_from_file(silecs_design_deploy_path) silecs_utils.check_silecs_version_mismatch(script_version=self.silecs_env._SILECS_VERSION, file_version=silecs_version) if extension == silecs_const.SILECSDESIGN: self.generate_silecsdesign(silecs_design_deploy_path) + makefile = silecs_utils.get_silecs_design_makefile_template(self.silecs_env.SILECS_BASE) elif extension == silecs_const.SILECSDEPLOY: self.generate_silecsdeploy(silecs_design_deploy_path) + # Snap7 path is one folder up from the silecs release path. + makefile = silecs_utils.get_silecs_deploy_makefile_template( + self.silecs_env.SILECS_BASE, os.path.dirname(self.silecs_env.SILECS_BASE)) + + with open(makefile_path, 'w') as fp: + fp.write(makefile) def silecs_generate(self, file_paths): for path in file_paths: @@ -461,8 +488,7 @@ class SilecsCli: print("INFO: Running final migration. Only basic xml-version strings will be changed") module = __import__("migrationBase") module.run_migrate(silecs_filepath, new_xml_schema, current_version, new_version, False) - - if not migration_modules: + elif not migration_modules: print(f"WARNING: There is no Migration Script for an update from: {old_version} to {new_version}\n" \ f"Using generic migration-script. Only basic xml-version strings will be changed.\n") module = __import__("migrationBase") diff --git a/silecs_cli/silecs_const.py b/silecs_cli/silecs_const.py index 9a6fc13d325e71e823fedc6b152afeec1db2c19b..fa39daa8fc300f684c03fe671c1f3cee4d829afa 100644 --- a/silecs_cli/silecs_const.py +++ b/silecs_cli/silecs_const.py @@ -9,6 +9,7 @@ SILECSDESIGN = "silecsdesign" SILECSDEPLOY = "silecsdeploy" DESIGN = "design" DEPLOY = "deploy" +SILECS_MAKEFILE = "Makefile.silecs" # Silecs modules needed to import to generate code SILECS_MODULES = ["genparam", "genplcsrc", "genduwrapper", "fillFESADeployUnit", "generateFesaDesign", "generateSourceCode"] diff --git a/silecs_cli/silecs_utils.py b/silecs_cli/silecs_utils.py index 42d93cd2c4739d1819f4bd65858169bf89f2a833..5c10cda900ec2dbf549c4534f0401a46168313ba 100644 --- a/silecs_cli/silecs_utils.py +++ b/silecs_cli/silecs_utils.py @@ -119,6 +119,27 @@ def create_backup_file(filepath): with open(backup_file, 'w+'): shutil.copyfile(src=filepath, dst=backup_file) +def get_silecs_design_makefile_template(silecs_base): + return f"""# This Makefile is auto-generated with SILECS generation tools. +# It should be included in the project's Makefile.specific by the user by adding the line 'include Makefile.silecs'. + +SILECS_BASE ?= {silecs_base} +COMPILER_FLAGS += -I$(SILECS_BASE)/silecs-communication-cpp/include +""" + +def get_silecs_deploy_makefile_template(silecs_base, snap7_base): + return f"""# This Makefile is auto-generated with SILECS generation tools. +# It should be included in the project's Makefile.specific by the user by adding the line 'include Makefile.silecs'. + +SILECS_BASE ?= {silecs_base} +SNAP7_BASE ?= {snap7_base} +LINKER_FLAGS += -L$(SILECS_BASE)/silecs-communication-cpp/lib/$(CPU) -lsilecs-comm +LINKER_FLAGS += -L$(SNAP7_BASE)/snap7/latest/$(CPU)-linux -lsnap7 + +#add default search path for dynamic snap7 library +LINKER_FLAGS += -Wl,-rpath,$(SNAP7_BASE)/snap7/latest/$(CPU)-linux,-rpath,/usr/lib +""" + def get_silecs_design_template(project_name, silecs_version, design_schema_path): date = datetime.datetime.today().strftime('%m/%d/%Y') return f"""<?xml version="1.0" encoding="UTF-8"?>