Skip to content

Commit 3eb9517

Browse files
authored
Remove any platform specific code in code-generated CMakeLists (#128)
* Remove any platform specific code in code-generated CMakeLists * Fix FlexPRET * Fix generated CMake for POSIX * Little polishing
1 parent 51b887e commit 3eb9517

File tree

7 files changed

+43
-47
lines changed

7 files changed

+43
-47
lines changed

examples/flexpret/CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ if(NOT DEFINED ENV{FP_SDK_PATH})
44
message(FATAL_ERROR "FP_SDK_PATH environment variable not set!")
55
endif()
66

7+
set(PLATFORM "FLEXPRET" CACHE STRING "Set platform to FlexPRET")
8+
79
include($ENV{FP_SDK_PATH}/cmake/riscv-toolchain.cmake)
810
include($ENV{FP_SDK_PATH}/cmake/fp-app.cmake)
911

1012
project(fp-lf)
1113

12-
add_executable(fp-smoke src/main.c)
13-
add_subdirectory(src-gen/Smoke)
14-
target_link_libraries(fp-smoke PUBLIC Smoke)
14+
include(src-gen/Smoke/CMakeLists.txt)
15+
add_subdirectory(${REACTOR_UC_PATH})
16+
17+
add_executable(fp-smoke main.c ${LF_SOURCES})
18+
target_link_libraries(fp-smoke PUBLIC reactor-uc)
19+
target_include_directories(fp-smoke PRIVATE ${LF_INCLUDE_DIRS})
20+
1521
fp_add_outputs(fp-smoke)
File renamed without changes.

examples/zephyr/hello_lf/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.20.0)
22
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
33
project(HelloLF)
44

5-
target_sources(app PRIVATE src/main.c)
6-
add_subdirectory(src-gen/HelloLF)
7-
target_link_libraries(app PRIVATE HelloLF)
5+
set(PLATFORM "ZEPHYR" CACHE STRING "Set platform to Zephyr")
6+
include(src-gen/HelloLF/CMakeLists.txt)
7+
add_subdirectory(${REACTOR_UC_PATH})
8+
9+
target_sources(app PRIVATE main.c ${LF_SOURCES})
10+
target_link_libraries(app PRIVATE reactor-uc)
11+
target_include_directories(app PRIVATE ${LF_INCLUDE_DIRS})
File renamed without changes.

lfc/core/src/main/kotlin/org/lflang/generator/uc/UcCmakeGenerator.kt

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,46 @@ import java.nio.file.Path
1515
class UcCmakeGenerator(private val targetConfig: TargetConfig, private val fileConfig: FileConfig) {
1616
private val S = '$' // a little trick to escape the dollar sign with $S
1717
private val platform = targetConfig.get(PlatformProperty.INSTANCE).platform
18-
private val platformName = if (platform == PlatformType.Platform.AUTO) "POSIX" else platform.name.uppercase()
1918

20-
fun generatePlatformSpecific() = with(PrependOperator) {
19+
fun generateCmake(sources: List<Path>) =
2120
if (platform == PlatformType.Platform.AUTO) {
22-
"""
23-
|# For POSIX we directly generate an executable.
24-
|# and install it to the bin directory.
25-
|add_executable($S{LF_MAIN_TARGET} $S{SOURCES})
26-
|install(TARGETS $S{LF_MAIN_TARGET}
27-
| RUNTIME DESTINATION $S{CMAKE_INSTALL_BINDIR}
28-
| OPTIONAL
29-
|)
30-
""".trimMargin()
31-
} else if (platform == PlatformType.Platform.ZEPHYR) {
32-
"""
33-
|# For Zephyr we generate a library that can be included
34-
|# in a Zephyr project.
35-
|zephyr_library_named($S{LF_MAIN_TARGET})
36-
|zephyr_library_sources($S{SOURCES})
37-
|zephyr_library_link_libraries(kernel)
38-
""".trimMargin()
39-
} else if (platform == PlatformType.Platform.FLEXPRET){
40-
"""
41-
|add_library($S{LF_MAIN_TARGET} $S{SOURCES})
42-
|target_link_libraries($S{LF_MAIN_TARGET} PUBLIC fp-sdk)
43-
""".trimMargin()
21+
generateCmakePosix(sources)
4422
} else {
45-
"""
46-
|add_library($S{LF_MAIN_TARGET} $S{SOURCES})
47-
""".trimMargin()
48-
23+
generateCmakeEmbedded(sources)
4924
}
25+
26+
fun generateCmakeEmbedded(sources: List<Path>) = with(PrependOperator) {
27+
"""
28+
|# This file is generated by LFC. It is meant to be included in
29+
|# an existing CMake project.
30+
|
31+
|set(LF_SOURCES
32+
${" | "..sources.joinWithLn { "$S{CMAKE_CURRENT_LIST_DIR}/${it.toUnixString()}"}}
33+
|)
34+
|set(REACTOR_UC_PATH $S{CMAKE_CURRENT_LIST_DIR}/reactor-uc)
35+
|set(LF_INCLUDE_DIRS $S{CMAKE_CURRENT_LIST_DIR})
36+
""".trimMargin()
5037
}
51-
fun generateCmake(sources: List<Path>) = with(PrependOperator) {
38+
39+
fun generateCmakePosix(sources: List<Path>) = with(PrependOperator) {
5240
"""
5341
|cmake_minimum_required(VERSION 3.5)
5442
|project(${fileConfig.name} VERSION 0.0.0 LANGUAGES C)
55-
|set(PLATFORM $platformName CACHE STRING "Target platform")
56-
|
57-
|# Get the value of the environment variable
58-
|if(NOT DEFINED ENV{REACTOR_UC_PATH})
59-
| message(FATAL_ERROR "Environment variable REACTOR_UC_PATH is not set. Please source the env.bash in reactor-uc.")
60-
|endif()
43+
|set(PLATFORM POSIX CACHE STRING "Target platform")
6144
|
6245
|set(LF_MAIN_TARGET ${fileConfig.name})
6346
|set(SOURCES
6447
${" | "..sources.joinWithLn { it.toUnixString() }}
6548
|)
66-
${" |"..generatePlatformSpecific()}
49+
|add_executable($S{LF_MAIN_TARGET} $S{SOURCES})
50+
|install(TARGETS $S{LF_MAIN_TARGET}
51+
| RUNTIME DESTINATION $S{CMAKE_INSTALL_BINDIR}
52+
| OPTIONAL
53+
|)
6754
|
6855
|add_subdirectory(reactor-uc)
69-
|
70-
|target_include_directories($S{LF_MAIN_TARGET} PUBLIC $S{CMAKE_CURRENT_SOURCE_DIR})
71-
|target_link_libraries($S{LF_MAIN_TARGET} PUBLIC reactor-uc)
56+
|target_link_libraries($S{LF_MAIN_TARGET} PRIVATE reactor-uc)
57+
|target_include_directories($S{LF_MAIN_TARGET} PRIVATE $S{CMAKE_CURRENT_LIST_DIR})
7258
|
7359
""".trimMargin()
7460
}

lfc/core/src/main/kotlin/org/lflang/generator/uc/UcFileConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class UcFileConfig(resource: Resource, srcGenBasePath: Path, useHierarchicalBin:
2222
)
2323

2424
/** Relative path to the directory where all source files for this resource should be generated in. */
25-
private fun getGenDir(r: Resource): Path = srcGenPath.resolve(r.name)
25+
private fun getGenDir(r: Resource): Path = this.getDirectory(r).resolve(r.name)
2626

2727
/** Path to the header file corresponding to this reactor */
2828
fun getReactorHeaderPath(r: Reactor): Path = getGenDir(r.eResource()).resolve("${r.name}.h")

lfc/core/src/main/kotlin/org/lflang/generator/uc/UcMakeGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class UcMakeGenerator(private val targetConfig: TargetConfig, private val fileCo
1313
fun generateMake(sources: List<Path>) = with(PrependOperator) {
1414
"""
1515
| # Makefile genrated for ${fileConfig.name}
16-
|LF_GEN_SOURCES = \
16+
|LF_SOURCES = \
1717
${" | "..sources.joinWithLn { it.toUnixString() + " \\ "}}
1818
|
1919
""".trimMargin()

0 commit comments

Comments
 (0)