Skip to content

Commit

Permalink
added compiler meta-info as info.h
Browse files Browse the repository at this point in the history
... and added missing Emscripten detection. Doesn't need to be explicitly set anymore -- emcmake suffices.

Signed-off-by: rstein <r.steinhagen@gsi.de>
  • Loading branch information
RalphSteinhagen committed Sep 27, 2023
1 parent ed71b4c commit 5e9d0a5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 41 deletions.
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,33 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

# Initialize a variable to hold all the compiler flags -> exported into global config.h(.in)
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(ALL_COMPILER_FLAGS "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS}")
elseif(CMAKE_BUILD_TYPE MATCHES Release)
set(ALL_COMPILER_FLAGS "${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS}")
elseif(CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
set(ALL_COMPILER_FLAGS "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${CMAKE_CXX_FLAGS}")
elseif(CMAKE_BUILD_TYPE MATCHES MinSizeRel)
set(ALL_COMPILER_FLAGS "${CMAKE_CXX_FLAGS_MINSIZEREL} ${CMAKE_CXX_FLAGS}")
endif()
# Replace ; with space
string(REPLACE ";" " " ALL_COMPILER_FLAGS "${ALL_COMPILER_FLAGS}")


# Mainly for FMT
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)

add_library(graph-prototype-options INTERFACE)
include(cmake/CompilerWarnings.cmake)
set_project_warnings(graph-prototype-options)


if(CMAKE_CXX_COMPILER MATCHES "/em\\+\\+(-[a-zA-Z0-9.])?$")
message(" Transpiling to WASM: using: Emscripten (${CMAKE_CXX_COMPILER})")
set(EMSCRIPTEN true)
endif()

if (EMSCRIPTEN)
set(CMAKE_EXECUTABLE_SUFFIX ".js")
target_compile_options(graph-prototype-options INTERFACE
Expand Down Expand Up @@ -103,6 +123,10 @@ target_include_directories(fftw INTERFACE ${FFTW_PREFIX}/install/include)
target_link_directories(fftw INTERFACE ${FFTW_PREFIX}/install/lib ${FFTW_PREFIX}/install/lib64)
add_dependencies(fftw fftw_ext)

# configure a header file to pass the CMake settings to the source code
configure_file("${PROJECT_SOURCE_DIR}/cmake/config.h.in" "${PROJECT_BINARY_DIR}/config.h" @ONLY)
include_directories("${PROJECT_BINARY_DIR}")

add_subdirectory(include)
add_subdirectory(src)

Expand Down
32 changes: 15 additions & 17 deletions bench/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
function(add_benchmark BM_NAME)
add_executable(${BM_NAME} ${BM_NAME}.cpp)
function(append_compiler_flags TARGET_NAME)
set(FLAGS_COMMON -Wall)
if (EMSCRIPTEN)
target_compile_options(${BM_NAME} PRIVATE -Wall)
target_link_options(${BM_NAME} PRIVATE -Wall)
set(FLAGS_SPECIAL "")
else ()
target_compile_options(${BM_NAME} PRIVATE -Wall -march=native)
target_link_options(${BM_NAME} PRIVATE -Wall -march=native)
set(FLAGS_SPECIAL -march=native)
endif ()
# target_compile_options(${BM_NAME} PRIVATE -Wall -march=native -fsanitize=address)
# target_link_options(${BM_NAME} PRIVATE -Wall -march=native -fsanitize=address)

target_compile_options(${TARGET_NAME} PRIVATE ${FLAGS_COMMON} ${FLAGS_SPECIAL})
target_link_options(${TARGET_NAME} PRIVATE ${FLAGS_COMMON} ${FLAGS_SPECIAL})
endfunction()

function(add_benchmark BM_NAME)
add_executable(${BM_NAME} ${BM_NAME}.cpp)
append_compiler_flags(${BM_NAME})
target_link_libraries(${BM_NAME} PRIVATE graph-prototype-options graph-prototype refl-cpp fmt ut fftw)
endfunction()

Expand All @@ -22,12 +26,6 @@ add_benchmark(bm_profiler)
add_benchmark(bm_scheduler)

add_executable(bm_case1_nosimd bm_case1.cpp)
if (EMSCRIPTEN)
target_compile_options(bm_case1_nosimd PRIVATE -Wall -DDISABLE_SIMD=1)
target_link_options(bm_case1_nosimd PRIVATE -Wall)
target_link_libraries(bm_case1_nosimd PRIVATE graph-prototype-options graph-prototype refl-cpp fmt ut)
else ()
target_compile_options(bm_case1_nosimd PRIVATE -Wall -march=native -DDISABLE_SIMD=1)
target_link_options(bm_case1_nosimd PRIVATE -Wall -march=native)
target_link_libraries(bm_case1_nosimd PRIVATE graph-prototype-options graph-prototype refl-cpp fmt ut)
endif ()
append_compiler_flags(bm_case1_nosimd)
target_compile_options(bm_case1_nosimd PRIVATE -DDISABLE_SIMD=1)
target_link_libraries(bm_case1_nosimd PRIVATE graph-prototype-options graph-prototype refl-cpp fmt ut)
5 changes: 5 additions & 0 deletions cmake/CompilerWarnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ function(set_project_warnings project_name)
message(AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.")
endif()

# Replace semicolons with spaces in PROJECT_WARNINGS
string(REPLACE ";" " " PROJECT_WARNINGS_MOD "${PROJECT_WARNINGS}")
set(${output_var} "${PROJECT_WARNINGS}" PARENT_SCOPE)
set(ALL_COMPILER_FLAGS "${ALL_COMPILER_FLAGS}${PROJECT_WARNINGS_MOD}" PARENT_SCOPE)

target_compile_options(${project_name} INTERFACE ${PROJECT_WARNINGS})

endfunction()
10 changes: 10 additions & 0 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef GNURADIO_GRAPH_PROJECT_CONFIG
#define GNURADIO_GRAPH_PROJECT_CONFIG

#define CXX_COMPILER_PATH "@CMAKE_CXX_COMPILER@"
#define CXX_COMPILER_ARG1 "@CMAKE_CXX_COMPILER_ARG1@"
#define CXX_COMPILER_ID "@CMAKE_CXX_COMPILER_ID@"
#define CXX_COMPILER_VERSION "@CMAKE_CXX_COMPILER_VERSION@"
#define CXX_COMPILER_FLAGS "@ALL_COMPILER_FLAGS@"

#endif // GNURADIO_GRAPH_PROJECT_CONFIG
6 changes: 6 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <cassert>

#include <config.h> // contains the project and compiler flags definitions

#include <graph.hpp>

namespace fg = fair::graph;
Expand Down Expand Up @@ -103,6 +105,10 @@ main() {
using fg::merge;
using fg::merge_by_index;

fmt::print("Project compiler: '{}' - version '{}'\n", CXX_COMPILER_ID, CXX_COMPILER_VERSION);
fmt::print("Project compiler path: '{}' - arg1 '{}'\n", CXX_COMPILER_PATH, CXX_COMPILER_ARG1);
fmt::print("Project compiler flags: '{}'\n", CXX_COMPILER_FLAGS);

{
// declare flow-graph: 2 x in -> adder -> scale-by-2 -> scale-by-minus1 -> output
auto merged = merge_by_index<0, 0>(scale<int, -1>(), merge_by_index<0, 0>(scale<int, 2>(), adder<int>()));
Expand Down
47 changes: 23 additions & 24 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@ set(TESTS_SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR})

configure_file(build_configure.hpp.in build_configure.hpp @ONLY)

# Define common compile and link options
set(COMMON_COMPILE_OPTIONS -Wall)
set(COMMON_LINK_OPTIONS -Wall)
if (NOT (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")) # needed for clang15 (false positives, fixed in clang16)
list(APPEND COMMON_COMPILE_OPTIONS -fsanitize=address)
list(APPEND COMMON_LINK_OPTIONS -fsanitize=address)
endif()

function(setup_test TARGET_NAME)
target_compile_options(${TARGET_NAME} PRIVATE ${COMMON_COMPILE_OPTIONS})
target_link_options(${TARGET_NAME} PRIVATE ${COMMON_LINK_OPTIONS})
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_BINARY_DIR}/include ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(${TARGET_NAME} PRIVATE graph-prototype-options graph-prototype fmt refl-cpp ut fftw)
add_test(NAME ${TARGET_NAME} COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME})
endfunction()

function(add_ut_test TEST_NAME)
add_executable(${TEST_NAME} ${TEST_NAME}.cpp)
if ((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")) # needed for clang15 (false positives, fixed in clang16)
target_compile_options(${TEST_NAME} PRIVATE -Wall)
target_link_options(${TEST_NAME} PRIVATE -Wall)
else ()
target_compile_options(${TEST_NAME} PRIVATE -fsanitize=address -Wall)
target_link_options(${TEST_NAME} PRIVATE -fsanitize=address -Wall)
endif ()
target_include_directories(${TEST_NAME} PRIVATE ${CMAKE_BINARY_DIR}/include ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(${TEST_NAME} PRIVATE graph-prototype-options graph-prototype fmt refl-cpp ut fftw)
add_test(NAME ${TEST_NAME} COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} ${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME})
setup_test(${TEST_NAME})
endfunction()

function(add_app_test TEST_NAME)
add_executable(${TEST_NAME} ${TEST_NAME}.cpp)
target_compile_options(${TEST_NAME} PRIVATE -fsanitize=address -Wall)
target_include_directories(${TEST_NAME} PRIVATE ${CMAKE_BINARY_DIR}/include ${CMAKE_CURRENT_BINARY_DIR})
target_link_options(${TEST_NAME} PRIVATE -fsanitize=address -Wall)
target_link_libraries(${TEST_NAME} PRIVATE graph-prototype-options graph-prototype graph-prototype-plugin fmt refl-cpp fftw)
setup_test(${TEST_NAME})
target_link_libraries(${TEST_NAME} PRIVATE graph-prototype-plugin)
add_dependencies(${TEST_NAME} good_math_plugin good_base_plugin bad_plugin)
add_test(NAME ${TEST_NAME} COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} ${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME})
endfunction()

add_ut_test(qa_buffer)
Expand All @@ -47,20 +51,15 @@ if (NOT EMSCRIPTEN)
add_subdirectory(plugins)

if (NOT (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang"))
# add add target manually without asan TODO: reenable once it builds on the CI again
# add_ut_test(qa_plugins_test)
# add target manually without asan
add_executable(qa_plugins_test qa_plugins_test.cpp)
target_compile_options(qa_plugins_test PRIVATE -Wall)
target_link_options(qa_plugins_test PRIVATE -Wall)
target_include_directories(qa_plugins_test PRIVATE ${CMAKE_BINARY_DIR}/include ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(qa_plugins_test PRIVATE graph-prototype-options graph-prototype fmt refl-cpp ut fftw)
add_test(NAME qa_plugins_test COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} ${CMAKE_CURRENT_BINARY_DIR}/qa_plugins_test)
setup_test(qa_plugins_test)
target_link_libraries(qa_plugins_test PRIVATE graph-prototype-plugin)
add_dependencies(qa_plugins_test good_math_plugin good_base_plugin bad_plugin)
endif ()
endif()

add_app_test(app_plugins_test app_plugins_test.cpp)

add_app_test(app_grc)
target_link_libraries(app_grc PRIVATE yaml-cpp::yaml-cpp)
endif ()
endif()

0 comments on commit 5e9d0a5

Please sign in to comment.