Skip to content

Commit

Permalink
added compiler meta-info as info.h (#173)
Browse files Browse the repository at this point in the history
* added compiler meta-info as info.h

... and added missing Emscripten detection if cmake is used directly as crosss-compiling against em++ and not using the emcmake cmake ...

Signed-off-by: rstein <r.steinhagen@gsi.de>
  • Loading branch information
RalphSteinhagen authored Sep 28, 2023
1 parent ed71b4c commit b2d5c75
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 47 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.])?$") # if this hasn't been set before via e.g. emcmake
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
26 changes: 25 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,34 @@ pmt_dep = libpmtv.get_variable('pmt_dep')
graph_prototype_options = []
if meson.is_cross_build()
if meson.get_external_property('EMSCRIPTEN', false)
graph_prototype_options = ['-s','ALLOW_MEMORY_GROWTH=1']
graph_prototype_options = ['-s','ALLOW_MEMORY_GROWTH=1','-fwasm-exceptions','-pthread','SHELL:-s PTHREAD_POOL_SIZE=30"']
endif
endif

# Determine compiler flags based on build type
all_compiler_flags = ''
if get_option('buildtype') == 'debug'
all_compiler_flags = '-g'
elif get_option('buildtype') == 'release'
all_compiler_flags = '-O3'
elif get_option('buildtype') == 'debugoptimized'
all_compiler_flags = '-O2 -g'
elif get_option('buildtype') == 'minsize'
all_compiler_flags = '-Os'
endif

# Additional compiler flags from the global arguments
all_compiler_flags += ' ' + ' '.join(gcc_warnings) + ' '.join(graph_prototype_options)

# Configure the header file
config_h_data = configuration_data()
config_h_data.set('CMAKE_CXX_COMPILER', compiler.get_id())
config_h_data.set('CMAKE_CXX_COMPILER_ARG1', '')
config_h_data.set('CMAKE_CXX_COMPILER_ID', compiler.get_id())
config_h_data.set('CMAKE_CXX_COMPILER_VERSION', compiler.version())
config_h_data.set('ALL_COMPILER_FLAGS', all_compiler_flags)
configure_file(input : 'cmake/config.h.in', output : 'config.h', configuration : config_h_data)

subdir('include')
subdir('src')
if (get_option('enable_testing'))
Expand Down
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
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

cpp_args = graph_prototype_options
main_exe = executable('main', 'main.cpp',
include_directories: '..',
dependencies : [graph_dep, fmt_dep, reflcpp_dep],
cpp_args: graph_prototype_options,
link_args: graph_prototype_options,
Expand Down
50 changes: 21 additions & 29 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ set(TESTS_SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR})

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

function(setup_test_no_asan TARGET_NAME)
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(setup_test TARGET_NAME)
target_compile_options(${TARGET_NAME} PRIVATE "-fsanitize=address")
target_link_options(${TARGET_NAME} PRIVATE "-fsanitize=address")
setup_test_no_asan(${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 @@ -43,24 +43,16 @@ add_ut_test(qa_thread_affinity)
add_ut_test(qa_thread_pool)
add_ut_test(qa_traits)

if (NOT EMSCRIPTEN)
if (NOT (EMSCRIPTEN OR (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")))
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_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)
target_link_libraries(qa_plugins_test PRIVATE graph-prototype-plugin)
add_dependencies(qa_plugins_test good_math_plugin good_base_plugin bad_plugin)
endif ()
add_executable(qa_plugins_test qa_plugins_test.cpp)
setup_test_no_asan(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)

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 b2d5c75

Please sign in to comment.