Skip to content

Commit

Permalink
Have LFC generate a main function that can optionally be included in …
Browse files Browse the repository at this point in the history
…the build
  • Loading branch information
erlingrj committed Dec 8, 2024
1 parent 1231bbd commit 0584253
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 49 deletions.
2 changes: 1 addition & 1 deletion examples/flexpret/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ project(fp-lf)
include(src-gen/Smoke/CMakeLists.txt)
add_subdirectory(${REACTOR_UC_PATH})

add_executable(fp-smoke main.c ${LFC_GEN_SOURCES})
add_executable(fp-smoke ${LFC_GEN_MAIN} ${LFC_GEN_SOURCES})
target_link_libraries(fp-smoke PUBLIC reactor-uc)
target_include_directories(fp-smoke PRIVATE ${LFC_GEN_INCLUDE_DIRS})

Expand Down
5 changes: 0 additions & 5 deletions examples/flexpret/main.c

This file was deleted.

5 changes: 5 additions & 0 deletions examples/riot/buildAll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ for board in "${BOARDS[@]}"; do
popd
done
done

# Build lf example
pushd hello_lf
run/build.sh
popd
5 changes: 0 additions & 5 deletions examples/riot/hello_lf/main.c

This file was deleted.

4 changes: 4 additions & 0 deletions examples/riot/hello_lf/run/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/env bash

${REACTOR_UC_PATH}/lfc/bin/lfc-dev src/HelloLF.lf
make all
7 changes: 6 additions & 1 deletion examples/zephyr/buildAll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ for dir in "${FOLDERS[@]}"; do
pushd $dir
$COMMAND
popd
done
done

# Build lf example
pushd hello_lf
run/build.sh
popd
4 changes: 2 additions & 2 deletions examples/zephyr/hello_lf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ 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_sources(app PRIVATE ${LFC_GEN_MAIN} ${LFC_GEN_SOURCES})
target_link_libraries(app PRIVATE reactor-uc)
target_include_directories(app PRIVATE ${LF_INCLUDE_DIRS})
target_include_directories(app PRIVATE ${LFC_GEN_INCLUDE_DIRS})
5 changes: 0 additions & 5 deletions examples/zephyr/hello_lf/main.c

This file was deleted.

4 changes: 4 additions & 0 deletions examples/zephyr/hello_lf/run/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/env bash

${REACTOR_UC_PATH}/lfc/bin/lfc-dev src/HelloLF.lf
west build -b qemu_cortex_m3 -p always
4 changes: 1 addition & 3 deletions examples/zephyr/hello_lf/src/HelloLF.lf
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
target uC {
platform: Zephyr
}
target uC

main reactor {
reaction(startup) {=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.lflang.unreachable
import org.lflang.util.FileUtil
import java.nio.file.Path
import java.time.LocalDateTime
import kotlin.io.path.name
import kotlin.math.max

class UcCmakeGenerator(private val main: Reactor, private val targetConfig: TargetConfig, private val fileConfig: FileConfig) {
Expand All @@ -38,8 +39,9 @@ class UcCmakeGenerator(private val main: Reactor, private val targetConfig: Targ
|# an existing CMake project.
|
|set(LFC_GEN_SOURCES
${" | "..sources.joinWithLn { "$S{CMAKE_CURRENT_LIST_DIR}/${it.toUnixString()}"}}
${" | "..sources.filterNot{it.name == "lf_main.c"}.joinWithLn { "$S{CMAKE_CURRENT_LIST_DIR}/${it.toUnixString()}"}}
|)
|set(LFC_GEN_MAIN "$S{CMAKE_CURRENT_LIST_DIR}/lf_main.c")
|set(REACTOR_UC_PATH $S{CMAKE_CURRENT_LIST_DIR}/reactor-uc)
|set(LFC_GEN_INCLUDE_DIRS $S{CMAKE_CURRENT_LIST_DIR})
|set(REACTION_QUEUE_SIZE ${main.getReactionQueueSize()} CACHE STRING "Size of the reaction queue")
Expand Down
31 changes: 11 additions & 20 deletions lfc/core/src/main/kotlin/org/lflang/generator/uc/UcMainGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,14 @@ class UcMainGenerator(
) {

private val ucParameterGenerator = UcParameterGenerator(main)
// For the default POSIX platform we generate a main function, this is only for simplifing
// quick testing. For real applications the code-generated sources must be included in an
// existing project.
fun generateMainFunction() = with(PrependOperator) {
if (targetConfig.get(PlatformProperty.INSTANCE).platform == PlatformType.Platform.NATIVE) {
"""
|// The following is to support convenient compilation of LF programs
|// targeting POSIX. For programs targeting embedded platforms a
|// main function is not generated.
|int main(int argc, char **argv) {
| lf_start();
|}
""".trimMargin()
} else {
""
}
}

fun getDuration() = if (targetConfig.isSet(TimeOutProperty.INSTANCE)) targetConfig.get(TimeOutProperty.INSTANCE).toCCode() else "FOREVER"

fun keepAlive() = if(targetConfig.isSet(KeepaliveProperty.INSTANCE)) "true" else "false"

fun fast() = if(targetConfig.isSet(FastProperty.INSTANCE)) "true" else "false"

fun generateMainSource() = with(PrependOperator) {
fun generateStartSource() = with(PrependOperator) {
"""
|#include "reactor-uc/reactor-uc.h"
|#include "${fileConfig.getReactorHeaderPath(main).toUnixString()}"
Expand All @@ -63,11 +46,10 @@ class UcMainGenerator(
| lf_environment.start(&lf_environment);
| lf_exit();
|}
${" |"..generateMainFunction()}
""".trimMargin()
}

fun generateMainHeader() = with(PrependOperator) {
fun generateStartHeader() = with(PrependOperator) {
"""
|#ifndef REACTOR_UC_LF_MAIN_H
|#define REACTOR_UC_LF_MAIN_H
Expand All @@ -78,4 +60,13 @@ class UcMainGenerator(
|
""".trimMargin()
}

fun generateMainSource() = with(PrependOperator) {
"""
|#include "lf_start.h"
|int main(void) {
| lf_start();
|}
""".trimMargin()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ import org.lflang.target.property.BuildTypeProperty
import org.lflang.toUnixString
import java.nio.file.Path
import java.time.LocalDateTime
import kotlin.io.path.name
import kotlin.math.max

class UcMakeGenerator(private val main: Reactor, private val targetConfig: TargetConfig, private val fileConfig: FileConfig) {
private val S = '$' // a little trick to escape the dollar sign with $S
fun generateMake(sources: List<Path>) = with(PrependOperator) {
val sources = sources.filterNot { it.name=="lf_main.c" }
"""
| # Makefile generated for ${fileConfig.name}
|LFC_GEN_SOURCES = \
${" | "..sources.joinWithLn { it.toUnixString() + if (it != sources.last()) " \\" else ""}}
|LFC_GEN_MAIN = lf_main.c
|REACTION_QUEUE_SIZE = ${max(main.getReactionQueueSize(), 1)}
|EVENT_QUEUE_SIZE = ${max(main.getEventQueueSize(), 1)}
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ class UcStandaloneGenerator(generator: UcGenerator, val srcGenPath: Path) :
// generate the main source file (containing main())
val mainGenerator = UcMainGenerator(mainReactor, generator.targetConfig, generator.fileConfig)

val startSourceFile = Paths.get("lf_start.c")
val startHeaderFile = Paths.get("lf_start.h")
val mainSourceFile = Paths.get("lf_main.c")
val mainHeaderFile = Paths.get("lf_main.h")

val startCodeMap = CodeMap.fromGeneratedCode(mainGenerator.generateStartSource())
val mainCodeMap = CodeMap.fromGeneratedCode(mainGenerator.generateMainSource())

ucSources.add(mainSourceFile)
ucSources.addAll(listOf(startSourceFile, mainSourceFile))
codeMaps[srcGenPath.resolve(startSourceFile)] = startCodeMap
codeMaps[srcGenPath.resolve(mainSourceFile)] = mainCodeMap

println("Path: $srcGenPath $srcGenPath")

FileUtil.writeToFile(startCodeMap.generatedCode, srcGenPath.resolve(startSourceFile), true)
FileUtil.writeToFile(mainCodeMap.generatedCode, srcGenPath.resolve(mainSourceFile), true)
FileUtil.writeToFile(mainGenerator.generateMainHeader(), srcGenPath.resolve(mainHeaderFile), true)
FileUtil.writeToFile(mainGenerator.generateStartHeader(), srcGenPath.resolve(startHeaderFile), true)

val cmakeGenerator = UcCmakeGenerator(mainReactor, targetConfig, generator.fileConfig)
val makeGenerator = UcMakeGenerator(mainReactor, targetConfig, generator.fileConfig)
Expand Down
5 changes: 4 additions & 1 deletion make/riot/riot.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ ifdef SRC_GEN_PATH
include $(SRC_GEN_PATH)/Makefile

# Include generated c files
SRC += $(patsubst %, $(SRC_GEN_PATH)/%, $(LFC_GEN_SOURCES)) main.c
SRC += $(patsubst %, $(SRC_GEN_PATH)/%, $(LFC_GEN_SOURCES))

# Include generated main file
SRC += $(SRC_GEN_PATH)/${LFC_GEN_MAIN}

# Include generated h files
CFLAGS += -I$(SRC_GEN_PATH)
Expand Down

0 comments on commit 0584253

Please sign in to comment.