Skip to content

Commit

Permalink
Merge pull request #513 from lf-lang/ccpp-target
Browse files Browse the repository at this point in the history
CCpp target
  • Loading branch information
Soroosh129 authored Sep 22, 2021
2 parents b1b41e6 + 615d0e0 commit 7887666
Show file tree
Hide file tree
Showing 55 changed files with 985 additions and 997 deletions.
7 changes: 3 additions & 4 deletions example/C/src/ROS/ROSBuiltInSerialization.lf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* This example showcases the infrastructure that is built into LF
* C runtime that can automatically serialize and deserialize ROS2
* This example showcases the infrastructure that is built into the
* CCpp target that can automatically serialize and deserialize ROS2
* messages in federated programs.
*
* This example contains a sender-receiver federated program in which
Expand All @@ -21,11 +21,10 @@
*
* @author Soroush Bateni <[email protected]>
*/
target C {
target CCpp {
cmake: true, // Only CMake is supported
cmake-include: "include/CMakeListsExtension.txt",
threads: 1,
compiler: "g++"
};

preamble {=
Expand Down
3 changes: 1 addition & 2 deletions example/C/src/ROS/ROSSerialization.lf
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
* There is a variant of this example called ROSBuiltInSerialization.lf that uses
* the built-in ROS 2 serialization feature that is provided in LF.
*/
target C {
target CCpp {
coordination: decentralized,
timeout: 10 sec,
cmake-include: "include/CMakeListsExtension.txt",
files: ["include/ament_target_dependencies.cmake"],
compiler: "g++",
};

preamble {=
Expand Down
4 changes: 3 additions & 1 deletion example/C/src/Rhythm/PlayWaveform.lf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ target C {
"/lib/C/util/audio_loop_linux.c",
"sounds/Bass-Drum-1.wav", "sounds/Hi-Bongo.wav", "sounds/Claves.wav", "sounds/High-Conga-1.wav",
"sounds/Cowbell-1.wav", "sounds/Cuica-1.wav", "sounds/Guiro.wav",
"sounds/Ensoniq-ESQ-1-Snare.wav", "sounds/Floor-Tom-1.wav"]
"sounds/Ensoniq-ESQ-1-Snare.wav", "sounds/Floor-Tom-1.wav"],
// Include the cmake support for the audio loop
cmake-include: ["/lib/C/util/audio_loop.cmake"]
}
preamble {=
#include "wave_file_reader.c"
Expand Down
20 changes: 8 additions & 12 deletions example/C/src/Rhythm/Rhythm.lf
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,20 @@
*/
target C {
threads: 2,
cmake-include: [
"sound-extension.cmake",
"audio_loop.cmake",
"sensor_simulator.cmake",
"ncurses-extension.cmake",
"mlib-extension.cmake"],
tracing: true,
files: [
"/lib/C/util/sensor_simulator.c", "/lib/C/util/sensor_simulator.h", "/lib/C/util/sensor_simulator.cmake",
"/lib/C/util/sensor_simulator.c", "/lib/C/util/sensor_simulator.h",
"/lib/C/util/wave_file_reader.c", "/lib/C/util/wave_file_reader.h",
"/lib/C/util/audio_loop_mac.c", "/lib/C/util/audio_loop.h", "/lib/C/util/audio_loop.cmake",
"/lib/C/util/audio_loop_mac.c", "/lib/C/util/audio_loop.h",
"/lib/C/util/audio_loop_linux.c",
"sounds/Bass-Drum-1.wav", "sounds/Hi-Bongo.wav", "sounds/Claves.wav", "sounds/High-Conga-1.wav",
"sounds/Cowbell-1.wav", "sounds/Cuica-1.wav", "sounds/Guiro.wav",
"sounds/Ensoniq-ESQ-1-Snare.wav", "sounds/Floor-Tom-1.wav",
"include/ncurses-extension.cmake",
"include/sound-extension.cmake",
"include/mlib-extension.cmake"]
"sounds/Ensoniq-ESQ-1-Snare.wav", "sounds/Floor-Tom-1.wav"],
// Include cmake support for some needed functionality
cmake-include: [
"/lib/C/util/sensor_simulator.cmake", // Enable support for the sensor simulator
"include/ncurses-extension.cmake", // Enable support for ncurses
"include/mlib-extension.cmake"] // Enable support for the math lib
};

import PlayWaveform from "PlayWaveform.lf"
Expand Down
15 changes: 3 additions & 12 deletions example/C/src/Rhythm/RhythmDistributed.lf
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,14 @@
*/
target C {
coordination: centralized,
cmake-include: [
"sound-extension.cmake",
"audio_loop.cmake",
"sensor_simulator.cmake",
"ncurses-extension.cmake",
"mlib-extension.cmake"],
files: [
"/lib/C/util/sensor_simulator.c", "/lib/C/util/sensor_simulator.h", "/lib/C/util/sensor_simulator.cmake",
"/lib/C/util/sensor_simulator.c", "/lib/C/util/sensor_simulator.h",
"/lib/C/util/wave_file_reader.c", "/lib/C/util/wave_file_reader.h",
"/lib/C/util/audio_loop_mac.c", "/lib/C/util/audio_loop.h", "/lib/C/util/audio_loop.cmake",
"/lib/C/util/audio_loop_mac.c", "/lib/C/util/audio_loop.h",
"/lib/C/util/audio_loop_linux.c",
"sounds/Bass-Drum-1.wav", "sounds/Hi-Bongo.wav", "sounds/Claves.wav", "sounds/High-Conga-1.wav",
"sounds/Cowbell-1.wav", "sounds/Cuica-1.wav", "sounds/Guiro.wav",
"sounds/Ensoniq-ESQ-1-Snare.wav", "sounds/Floor-Tom-1.wav",
"include/ncurses-extension.cmake",
"include/sound-extension.cmake",
"include/mlib-extension.cmake"]
"sounds/Ensoniq-ESQ-1-Snare.wav", "sounds/Floor-Tom-1.wav"]
};

import RhythmSource from "Rhythm.lf"
Expand Down
12 changes: 2 additions & 10 deletions example/C/src/Rhythm/RhythmDistributedNoUI.lf
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@
* @author Edward A. Lee
*/
target C {
cmake-include: [
"sound-extension.cmake",
"audio_loop.cmake",
"ncurses-extension.cmake",
"mlib-extension.cmake"],
coordination: decentralized,
timeout: 10 sec,
clock-sync: on,
Expand All @@ -47,14 +42,11 @@ target C {
},
files: [
"/lib/C/util/wave_file_reader.c", "/lib/C/util/wave_file_reader.h",
"/lib/C/util/audio_loop_mac.c", "/lib/C/util/audio_loop.h", "/lib/C/util/audio_loop.cmake",
"/lib/C/util/audio_loop_mac.c", "/lib/C/util/audio_loop.h",
"/lib/C/util/audio_loop_linux.c",
"sounds/Bass-Drum-1.wav", "sounds/Hi-Bongo.wav", "sounds/Claves.wav", "sounds/High-Conga-1.wav",
"sounds/Cowbell-1.wav", "sounds/Cuica-1.wav", "sounds/Guiro.wav",
"sounds/Ensoniq-ESQ-1-Snare.wav", "sounds/Floor-Tom-1.wav",
"include/ncurses-extension.cmake",
"include/sound-extension.cmake",
"include/mlib-extension.cmake"]
"sounds/Ensoniq-ESQ-1-Snare.wav", "sounds/Floor-Tom-1.wav"]
};
import PlayWaveform from "PlayWaveform.lf"

Expand Down
7 changes: 4 additions & 3 deletions example/C/src/Rhythm/SensorSimulator.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
*/
target C {
threads: 2,
cmake-include: ["ncurses-extension.cmake", "sensor_simulator.cmake"], // Adds support for ncurses
files: [
"/lib/C/util/sensor_simulator.c",
"/lib/C/util/sensor_simulator.h",
"/lib/C/util/sensor_simulator.cmake",
],
cmake-include: [
"/lib/C/util/sensor_simulator.cmake",
"include/ncurses-extension.cmake"
]
]
};
preamble {=
#include "sensor_simulator.h"
Expand Down
13 changes: 0 additions & 13 deletions example/C/src/Rhythm/include/sound-extension.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion example/ReflexGame/ReflexGameTest.lf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ main reactor {
// control-z) is received.
void* read_char(void* a) {
while(1) {
char* c = malloc(sizeof(char));
char* c = (char*)malloc(sizeof(char));
*c = getchar();
schedule_value(a, 0, c, 1);
if (*c == EOF) break;
Expand Down
30 changes: 29 additions & 1 deletion org.lflang.tests/src/org/lflang/tests/runtime/CTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
***************/
package org.lflang.tests.runtime;

import java.util.EnumSet;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import org.lflang.ASTUtils;
import org.lflang.Target;
import org.lflang.tests.TestRegistry.TestCategory;

/**
* Collection of tests for the C target.
Expand Down Expand Up @@ -101,4 +104,29 @@ public void runConcurrentTests() {
public void runFederatedTests() {
super.runFederatedTests();
}

/** Static description of test that runs C tests as CCpp. */
public static final String RUN_AS_CCPP_DESC = "Description: Running C tests as CCpp.";

/**
* Run C tests with the target CCpp.
*/
@Test
public void runAsCCpp() {
printTestHeader(RUN_AS_CCPP_DESC);

EnumSet<TestCategory> categories = EnumSet.allOf(TestCategory.class);

categories.removeAll(EnumSet.of(
// Don't need to test examples.
// If any of them uses CCpp, it will
// be tested when compileExamples is
// run.
TestCategory.EXAMPLE));

runTestsAndPrintResults(target,
categories::contains,
it -> ASTUtils.changeTargetName(it.fileConfig.resource, "CCpp"),
true);
}
}
11 changes: 9 additions & 2 deletions org.lflang/src/lib/C/util/audio_loop.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
target_sources(${LF_MAIN_TARGET} PRIVATE audio_loop_linux.c)
find_package( ALSA REQUIRED)
if (ALSA_FOUND)
include_directories( ${ALSA_INCLUDE_DIRS} )
target_link_libraries( ${LF_MAIN_TARGET} ${ALSA_LIBRARIES} )
endif(ALSA_FOUND)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
target_sources(${LF_MAIN_TARGET} PRIVATE audio_loop_mac.c)
target_link_libraries( ${LF_MAIN_TARGET} "-framework AudioToolbox" )
target_link_libraries( ${LF_MAIN_TARGET} "-framework CoreFoundation" )
else()
message(FATAL_ERROR "Your platform is not supported!"
" The C target supports Linux, MacOS and Windows.")
endif()
" Sound is only currently supported on Linux and MacOS.")
endif()
5 changes: 3 additions & 2 deletions org.lflang/src/lib/core/federated/clock-sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ ushort setup_clock_synchronization_with_rti() {

port_to_return = ntohs(federate_UDP_addr.sin_port);

// Set the option for this socket to reuse the same address
if (setsockopt(_lf_rti_socket_UDP, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0) {
// Set the option for this socket to reuse the same address
int option_value = 1;
if (setsockopt(_lf_rti_socket_UDP, SOL_SOCKET, SO_REUSEADDR, &option_value, sizeof(int)) < 0) {
error_print("Failed to set SO_REUSEADDR option on the socket: %s.", strerror(errno));
}
// Set the timeout on the UDP socket so that read and write operations don't block for too long
Expand Down
10 changes: 10 additions & 0 deletions org.lflang/src/lib/core/reactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,23 @@ do { \
* struct to true (which causes the object message to be sent),
* @param out The output port (by name).
*/
#ifndef __cplusplus
#define _LF_SET_NEW(out) \
do { \
out->is_present = true; \
lf_token_t* token = __set_new_array_impl(out->token, 1, out->num_destinations); \
out->value = token->value; \
out->token = token; \
} while(0)
#else
#define _LF_SET_NEW(out) \
do { \
out->is_present = true; \
lf_token_t* token = __set_new_array_impl(out->token, 1, out->num_destinations); \
out->value = static_cast<decltype(out->value)>(token->value); \
out->token = token; \
} while(0)
#endif // __cplusplus

/**
* Version of set() for output types given as 'type[]'.
Expand Down
10 changes: 10 additions & 0 deletions org.lflang/src/org/lflang/ASTUtils.xtend
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ class ASTUtils {
return true
}

/**
* Change the target name to 'newTargetName'.
* For example, change C to CCpp.
*/
static def boolean changeTargetName(Resource resource, String newTargetName) {
val r = resource.targetDecl
r.name = newTargetName
return true
}

/**
* Return true if any port on the left or right of the connection involves
* a bank of reactors or a multiport.
Expand Down
Loading

0 comments on commit 7887666

Please sign in to comment.