-
Notifications
You must be signed in to change notification settings - Fork 51
Description
The C++ code generation uses the function getRelativePath to generate include paths for definitions in the model:
| /** Gets the relative path for a file */ | |
| def getRelativePath(fileName: String): File.JavaPath = { | |
| val path = java.nio.file.Paths.get(fileName).toAbsolutePath.normalize | |
| removeLongestPathPrefix(path) | |
| } |
For example, the ArrayCppWriter calls this function to generate the header file for the array class corresponding to the FPP array definition:
fpp/compiler/lib/src/main/scala/codegen/CppWriter/ArrayCppWriter.scala
Lines 100 to 106 in 1ec4ecb
| private def getCppIncludes: CppDoc.Member = { | |
| val userHeaders = List( | |
| "Fw/Types/Assert.hpp", | |
| s"${s.getRelativePath(fileName).toString}.hpp" | |
| ).sorted.map(CppWriter.headerString).map(line) | |
| linesMember(Line.blank :: userHeaders, CppDoc.Lines.Cpp) | |
| } |
This usage of getRelativePath is not actually correct, because it generates paths relative to the current directory (where fpp-to-cpp is run) instead of relative to the location of the definition. We should replace this and other calls to getRelativePath with calls to a new function getNeighborPath(symbol, fileName). This function can work like getIncludePath, which is correct:
fpp/compiler/lib/src/main/scala/codegen/CppWriter/CppWriterState.scala
Lines 122 to 131 in 1ec4ecb
| def getIncludePath( | |
| sym: Symbol, | |
| fileNameBase: String, | |
| headerExtension: String = "hpp" | |
| ): String = { | |
| val loc = sym.getLoc.tuLocation | |
| val fullPath = loc.getNeighborPath(fileNameBase) | |
| val path = removeLongestPathPrefix(fullPath) | |
| s"${path.toString}.$headerExtension" | |
| } |
We can refactor getIncludePath to use this function.
I believe this bug or anomaly in the code generation has never been noticed because we always run fpp-to-cpp at the location of the definition, so the current directory is the parent directory for the definition. However, if you run fpp-to-cpp foo/bar.fpp, you can see that the headers are generated relative to ., not relative to foo.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status