Skip to content

Commit

Permalink
Make clang -fsanitize an option and fix setup script to add sanitize …
Browse files Browse the repository at this point in the history
…flag for clang
  • Loading branch information
mikkelfj committed Oct 25, 2023
1 parent f8c346f commit aaec13b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ option(FLATCC_TEST "enable tests" ON)
# project.
option(FLATCC_CXX_TEST "enable C++ tests" ON)

# Note that linking with flatcc debug libraries may require souce code to also use
# the sanitize flag.
option(FLATCC_DEBUG_CLANG_SANITIZE "enable clang sanitize flag for debug build" ON)

# Conditionally set project languages based on FLATCC_TEST, as C++ is
# only necessary if building the tests.
if (FLATCC_TEST AND FLATCC_CXX_TEST)
Expand Down Expand Up @@ -188,9 +192,11 @@ if (CMAKE_C_COMPILER_ID MATCHES "Clang" AND NOT "${CMAKE_CXX_SIMULATE_ID}" STREQ
if (FLATCC_IGNORE_CONST_COND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-tautological-constant-out-of-range-compare")
endif()
if (CMAKE_BUILD_TYPE MATCHES Debug)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
if (FLATCC_DEBUG_CLANG_SANITIZE)
if (CMAKE_BUILD_TYPE MATCHES Debug)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
endif()
endif()
# Suppress warning relaxed in clang-6, see https://reviews.llvm.org/D28148
if (CMAKE_C_COMPILER_VERSION VERSION_LESS 6)
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ fi

## Status

Release 0.6.2 (in development) is primarily a bug fix release, refer
to CHANGELOG for details.
Note that for clang debug builds, -fsanitize=undefined has been
added and this may require dependent source code to also use
that flag to avoid missing linker symbols. The feature can be disabled
in CMakeLists.txt.

Release 0.6.1 contains primarily bug fixes and numerous contributions
from the community to handle platform edge cases. Additionally,
pendantic GCC warnings are disabled, relying instead on clang, since GCC
Expand Down Expand Up @@ -390,6 +397,11 @@ different target platforms.

### Supported platforms (CI tested)

This list is somewhat outdated, more recent compiler versions are added and
some old ones are removed when CI platforms no longer supported but largely
the supported targets remain unchanged. MSVC 2010 might become deprecated
in the future.

The ci-more branch tests additional compilers:

- Ubuntu Trusty gcc 4.4, 4.6-4.9, 5, 6, 7 and clang 3.6, 3.8
Expand Down
13 changes: 12 additions & 1 deletion scripts/_user_build.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ mkdir -p generated

cd build

# we cannot link with debug library unless we use the sanitize flag.
SANFLAG_DEBUG=""
compiler_info="$($CC --version 2>&1)"
echo "using compiler: $compiler_info"
if echo $compiler_info | grep -q -i "clang"; then
SANFLAG_DEBUG="-fsanitize=undefined"
echo "sanitizer flag: $SANFLAG_DEBUG"
fi

if [[ "$FLATCC_PORTABLE" = "yes" ]]; then
CFLAGS="$CFLAGS -DFLATCC_PORTABLE"
fi

CFLAGS="$CFLAGS -I ${ROOT}/include -I ${ROOT}/generated"
CFLAGS_DEBUG=${CFLAGS_DEBUG:--g}
CFLAGS_DEBUG=${CFLAGS_DEBUG:--g $SANFLAG_DEBUG}
CFLAGS_RELEASE=${CFLAGS_RELEASE:--O2 -DNDEBUG}

${ROOT}/bin/flatcc -a -o ${ROOT}/generated ${ROOT}/src/*.fbs
Expand All @@ -30,3 +39,5 @@ $CC $CFLAGS $CFLAGS_DEBUG ${ROOT}/src/*.c ${ROOT}/lib/libflatccrt_d.a -o ${NAME}

echo "building '$NAME' for release"
$CC $CFLAGS $CFLAGS_RELEASE ${ROOT}/src/*.c ${ROOT}/lib/libflatccrt.a -o ${NAME}

echo using compiler $CC

0 comments on commit aaec13b

Please sign in to comment.