Skip to content

Commit

Permalink
Remove any platform specific code in code-generated CMakeLists (#128)
Browse files Browse the repository at this point in the history
* Remove any platform specific code in code-generated CMakeLists

* Fix FlexPRET

* Fix generated CMake for POSIX

* Little polishing
  • Loading branch information
erlingrj authored Nov 28, 2024
1 parent 51b887e commit 3eb9517
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 47 deletions.
12 changes: 9 additions & 3 deletions examples/flexpret/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ if(NOT DEFINED ENV{FP_SDK_PATH})
message(FATAL_ERROR "FP_SDK_PATH environment variable not set!")
endif()

set(PLATFORM "FLEXPRET" CACHE STRING "Set platform to FlexPRET")

include($ENV{FP_SDK_PATH}/cmake/riscv-toolchain.cmake)
include($ENV{FP_SDK_PATH}/cmake/fp-app.cmake)

project(fp-lf)

add_executable(fp-smoke src/main.c)
add_subdirectory(src-gen/Smoke)
target_link_libraries(fp-smoke PUBLIC Smoke)
include(src-gen/Smoke/CMakeLists.txt)
add_subdirectory(${REACTOR_UC_PATH})

add_executable(fp-smoke main.c ${LF_SOURCES})
target_link_libraries(fp-smoke PUBLIC reactor-uc)
target_include_directories(fp-smoke PRIVATE ${LF_INCLUDE_DIRS})

fp_add_outputs(fp-smoke)
File renamed without changes.
10 changes: 7 additions & 3 deletions examples/zephyr/hello_lf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(HelloLF)

target_sources(app PRIVATE src/main.c)
add_subdirectory(src-gen/HelloLF)
target_link_libraries(app PRIVATE HelloLF)
set(PLATFORM "ZEPHYR" CACHE STRING "Set platform to Zephyr")
include(src-gen/HelloLF/CMakeLists.txt)
add_subdirectory(${REACTOR_UC_PATH})

target_sources(app PRIVATE main.c ${LF_SOURCES})
target_link_libraries(app PRIVATE reactor-uc)
target_include_directories(app PRIVATE ${LF_INCLUDE_DIRS})
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,46 @@ import java.nio.file.Path
class UcCmakeGenerator(private val targetConfig: TargetConfig, private val fileConfig: FileConfig) {
private val S = '$' // a little trick to escape the dollar sign with $S
private val platform = targetConfig.get(PlatformProperty.INSTANCE).platform
private val platformName = if (platform == PlatformType.Platform.AUTO) "POSIX" else platform.name.uppercase()

fun generatePlatformSpecific() = with(PrependOperator) {
fun generateCmake(sources: List<Path>) =
if (platform == PlatformType.Platform.AUTO) {
"""
|# For POSIX we directly generate an executable.
|# and install it to the bin directory.
|add_executable($S{LF_MAIN_TARGET} $S{SOURCES})
|install(TARGETS $S{LF_MAIN_TARGET}
| RUNTIME DESTINATION $S{CMAKE_INSTALL_BINDIR}
| OPTIONAL
|)
""".trimMargin()
} else if (platform == PlatformType.Platform.ZEPHYR) {
"""
|# For Zephyr we generate a library that can be included
|# in a Zephyr project.
|zephyr_library_named($S{LF_MAIN_TARGET})
|zephyr_library_sources($S{SOURCES})
|zephyr_library_link_libraries(kernel)
""".trimMargin()
} else if (platform == PlatformType.Platform.FLEXPRET){
"""
|add_library($S{LF_MAIN_TARGET} $S{SOURCES})
|target_link_libraries($S{LF_MAIN_TARGET} PUBLIC fp-sdk)
""".trimMargin()
generateCmakePosix(sources)
} else {
"""
|add_library($S{LF_MAIN_TARGET} $S{SOURCES})
""".trimMargin()

generateCmakeEmbedded(sources)
}

fun generateCmakeEmbedded(sources: List<Path>) = with(PrependOperator) {
"""
|# This file is generated by LFC. It is meant to be included in
|# an existing CMake project.
|
|set(LF_SOURCES
${" | "..sources.joinWithLn { "$S{CMAKE_CURRENT_LIST_DIR}/${it.toUnixString()}"}}
|)
|set(REACTOR_UC_PATH $S{CMAKE_CURRENT_LIST_DIR}/reactor-uc)
|set(LF_INCLUDE_DIRS $S{CMAKE_CURRENT_LIST_DIR})
""".trimMargin()
}
fun generateCmake(sources: List<Path>) = with(PrependOperator) {

fun generateCmakePosix(sources: List<Path>) = with(PrependOperator) {
"""
|cmake_minimum_required(VERSION 3.5)
|project(${fileConfig.name} VERSION 0.0.0 LANGUAGES C)
|set(PLATFORM $platformName CACHE STRING "Target platform")
|
|# Get the value of the environment variable
|if(NOT DEFINED ENV{REACTOR_UC_PATH})
| message(FATAL_ERROR "Environment variable REACTOR_UC_PATH is not set. Please source the env.bash in reactor-uc.")
|endif()
|set(PLATFORM POSIX CACHE STRING "Target platform")
|
|set(LF_MAIN_TARGET ${fileConfig.name})
|set(SOURCES
${" | "..sources.joinWithLn { it.toUnixString() }}
|)
${" |"..generatePlatformSpecific()}
|add_executable($S{LF_MAIN_TARGET} $S{SOURCES})
|install(TARGETS $S{LF_MAIN_TARGET}
| RUNTIME DESTINATION $S{CMAKE_INSTALL_BINDIR}
| OPTIONAL
|)
|
|add_subdirectory(reactor-uc)
|
|target_include_directories($S{LF_MAIN_TARGET} PUBLIC $S{CMAKE_CURRENT_SOURCE_DIR})
|target_link_libraries($S{LF_MAIN_TARGET} PUBLIC reactor-uc)
|target_link_libraries($S{LF_MAIN_TARGET} PRIVATE reactor-uc)
|target_include_directories($S{LF_MAIN_TARGET} PRIVATE $S{CMAKE_CURRENT_LIST_DIR})
|
""".trimMargin()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class UcFileConfig(resource: Resource, srcGenBasePath: Path, useHierarchicalBin:
)

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

/** Path to the header file corresponding to this reactor */
fun getReactorHeaderPath(r: Reactor): Path = getGenDir(r.eResource()).resolve("${r.name}.h")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class UcMakeGenerator(private val targetConfig: TargetConfig, private val fileCo
fun generateMake(sources: List<Path>) = with(PrependOperator) {
"""
| # Makefile genrated for ${fileConfig.name}
|LF_GEN_SOURCES = \
|LF_SOURCES = \
${" | "..sources.joinWithLn { it.toUnixString() + " \\ "}}
|
""".trimMargin()
Expand Down

0 comments on commit 3eb9517

Please sign in to comment.