All Projects → jonathanpoelen → cpp-compiler-options

jonathanpoelen / cpp-compiler-options

Licence: MIT license
Compilation options for different versions of Clang, GCC and MSVC. Provided a generator and different file formats (cmake, xmake, meson, premake5, bjam/b2, ...)

Programming Languages

lua
6591 projects
Meson
512 projects
shell
77523 projects

Projects that are alternatives of or similar to cpp-compiler-options

Libosmscout
Libosmscout is a C++ library for offline map rendering, routing and location lookup based on OpenStreetMap data
Stars: ✭ 159 (+736.84%)
Mutual labels:  gcc, clang
Sol2
Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
Stars: ✭ 2,791 (+14589.47%)
Mutual labels:  gcc, clang
Polymcu
An open framework for micro-controller software
Stars: ✭ 173 (+810.53%)
Mutual labels:  gcc, clang
lldbg
A lightweight native GUI for LLDB.
Stars: ✭ 83 (+336.84%)
Mutual labels:  gcc, clang
C-Cpp-Coverage-for-CLion
Get coverage data in CLion using gcov or llvm-cov
Stars: ✭ 37 (+94.74%)
Mutual labels:  gcc, clang
C
Compile and execute C "scripts" in one go!
Stars: ✭ 1,920 (+10005.26%)
Mutual labels:  gcc, clang
Cmake Scripts
A selection of useful scripts for use in CMake projects, include code coverage, sanitizers, and dependency graph generation.
Stars: ✭ 202 (+963.16%)
Mutual labels:  gcc, clang
Vector
➿ A supercharged std::vector implementation (minus Allocator)
Stars: ✭ 118 (+521.05%)
Mutual labels:  gcc, clang
perses
Language-agnostic program reducer.
Stars: ✭ 57 (+200%)
Mutual labels:  gcc, clang
FrameOfReference
C++ library to pack and unpack vectors of integers having a small range of values using a technique called Frame of Reference
Stars: ✭ 36 (+89.47%)
Mutual labels:  gcc, clang
cdetect
🔬 Detect which compiler and compiler version a Linux executable (in the ELF format) was compiled with
Stars: ✭ 23 (+21.05%)
Mutual labels:  gcc, clang
c-compiler-security
Security-related flags and options for C compilers
Stars: ✭ 125 (+557.89%)
Mutual labels:  gcc, clang
Arm Cmake Toolchains
CMake toolchain configurations for ARM
Stars: ✭ 148 (+678.95%)
Mutual labels:  gcc, clang
ci playground
Playground for Cloud CI development for C++
Stars: ✭ 23 (+21.05%)
Mutual labels:  gcc, clang
Embedded Ide
IDE for C embedded development centered on bare-metal ARM systems
Stars: ✭ 127 (+568.42%)
Mutual labels:  gcc, clang
Fixed point
C++ Binary Fixed-Point Arithmetic
Stars: ✭ 199 (+947.37%)
Mutual labels:  gcc, clang
Cmakepchcompiler
CMake precompiled header support via custom PCH compiler extension
Stars: ✭ 105 (+452.63%)
Mutual labels:  gcc, clang
Moderncppci
This is an example of doing a Modern C++ project with CI
Stars: ✭ 109 (+473.68%)
Mutual labels:  gcc, clang
xcross
"Zero Setup" cross-compilation for C/C++. Supports numerous architectures, build systems, C standard libraries, vcpkg, and Conan.
Stars: ✭ 29 (+52.63%)
Mutual labels:  gcc, clang
minilib
A c standard system library with a focus on size, headeronly, "singlefile", intended for static linking. 187 Bytes for "Hello World"(regular elf), compiled with the standard gcc toolchain.
Stars: ✭ 29 (+52.63%)
Mutual labels:  gcc, clang

Compilation options for different versions of Clang, GCC, ICC, ICX and MSVC. Provided a generator and different file formats (build system and compiler).

The output directory contains files for cmake, xmake, premake5, meson, bjam/b2, scons and command-line options for gcc/g++, clang/clang++ and msvc. If a version of the compiler is not present, then there is no difference compared to an older version.

Each build system also has a branch with only the files it needs.

Here is an example with gcc:

int main()
{
  int x;
  return x; // used but uninitialized
}

$ g++ main.cpp

No output

$ g++ main.cpp @cpp-compiler-options/output/cpp/gcc/gcc-6.1-warnings

main.cpp: In function ‘int main()’:
main.cpp:4:10: warning: ‘x’ is used uninitialized in this function [-Wuninitialized]
    4 |   return x; // used but not initialized
      |          ^

(@file is a special option of gcc and clang for read command-line options from file.)

  1. Options
    1. Recommended options
  2. Use generated files
    1. CMake
    2. xmake
    3. Meson
    4. Premake5
    5. Bjam/B2 (Boost.Build)
    6. SCons
    7. Bash alias for gcc/clang
  3. Generators
    1. generators/compiler.lua
    2. generators/list_options.lua
    3. generators/{cmake,xmake,meson,premake5,bjam,scons}.lua
  4. How to add options?
    1. Update the options tree
      1. if_mt

Options

Supported options are (alphabetically in a category):

# Warning:

conversion_warnings = on default off sign conversion
covered_switch_default_warnings = on default off
fix_compiler_error = on default off
msvc_crt_secure_no_warnings = on default off
noexcept_warnings = default off on
reproducible_build_warnings = default off on
shadow_warnings = off default on local compatible_local all
suggestions = default off on
switch_warnings = on default off exhaustive_enum mandatory_default exhaustive_enum_and_mandatory_default
warnings = on default off strict very_strict
warnings_as_error = default off on basic
windows_abi_compatibility_warnings = off default on

# Pedantic:

msvc_conformance = all default all_without_throwing_new
pedantic = on default off as_error
stl_fix = on default off

# Debug:

debug = default off on line_tables_only gdb lldb sce
float_sanitizers = default off on
integer_sanitizers = default off on
other_sanitizers = default off thread pointer memory
sanitizers = default off on
stl_debug = default off on allow_broken_abi allow_broken_abi_and_bugs assert_as_exception

# Optimization:

cpu = default generic native
linker = default bfd gold lld native
lto = default off on fat thin
optimization = default 0 g 1 2 3 fast size z
whole_program = default off on strip_all

# C++:

exceptions = default off on
rtti = default off on

# Hardening:

control_flow = default off on branch return allow_bugs
relro = default off on full
stack_protector = default off on strong all

# Other:

color = default auto never always
coverage = default off on
diagnostics_format = default fixits patch print_source_range_info
diagnostics_show_template_tree = default off on
elide_type = default off on
msvc_isystem = default anglebrackets include_and_caexcludepath external_as_include_system_flag
msvc_isystem_with_template_from_non_external = default off on
pie = default off on static fpic fPIC fpie fPIE
windows_bigobj = on default

The value default does nothing.

If not specified, conversion_warnings, covered_switch_default_warnings, fix_compiler_error, msvc_crt_secure_no_warnings, pedantic, stl_fix, switch_warnings, warnings and windows_bigobj are on ; msvc_conformance are all ; shadow_warnings and windows_abi_compatibility_warnings are off.

  • control_flow=allow_bugs
    • clang: Can crash programs with "illegal hardware instruction" on totally unlikely lines. It can also cause link errors and force -fvisibility=hidden and -flto.
  • stl_debug=allow_broken_abi_and_bugs
    • clang: libc++ can crash on dynamic memory releases in the standard classes. This bug is fixed with the library associated with version 8.
  • msvc_isystem=external_as_include_system_flag is only available with cmake.

Recommended options

category options
debug control_flow=on
debug=on
sanitizers=on
stl_debug=allow_broken_abi or on
release cpu=native
linker=gold, lld or native
lto=on or thin
optimization=3
rtti=off
whole_program=strip_all
security control_flow=on
relro=full
stack_protector=strong
pie=PIE
really strict warnings pedantic=as_error
shadow_warnings=local
suggestions=on
warnings=very_strict

Use generated files

This is what enabled of sanitizers looks like with the different build systems available:

$ cmake -DJLN_SANITIZERS=on

$ xmake f --jln-sanitizers=on

$ premake5 --jln-sanitizers=on

$ meson -Djln_sanitizers=on

$ bjam -s jln_sanitizers=on

$ scons jln_sanitizers=on

(jln is a parameterizable prefix: ./compiler-options.lua generators/cmake.lua [prefix])

CMake

# launch example: cmake -DJLN_SANITIZERS=on
include(output/cpp/cmake)

# init default values
# jln_init_flags(
#     [jln-option> <default_value>]...
#     [AUTO_PROFILE on]
#     [VERBOSE on]
#     [BUILD_TYPE type [jln-option> <default_value>]...]...
# )
# AUTO_PROFILE: enables options based on CMAKE_BUILD_TYPE (assumes "Debug" if CMAKE_BUILD_TYPE is empty)
# BUILD_TYPE: enables following options only if ${CMAKE_BUILD_TYPE} has the same value (CMAKE_BUILD_TYPE assumed to Debug if empty)
jln_init_flags(
  SUGGESTIONS on                  # set SUGGESTIONS default value to "on"
  BUILD_TYPE debug SANITIZERS on  # set SANITIZERS default value to "on" only in Debug build
  BUILD_TYPE release LTO on       # set LTO default value to "on" only in Release build
)


# jln_target_interface(
#     <libname> {INTERFACE|PUBLIC|PRIVATE}
#     [<jln-option> <value>]...
#     [DISABLE_OTHERS {on|off}]
#     [BUILD_TYPE type [jln-option> <value>]...]...
# )
jln_target_interface(mytarget1 INTERFACE WARNINGS very_strict) # set WARNINGS to "very_strict"


# jln_flags(
#     CXX_VAR <out-variable>
#     LINK_VAR <out-variable>
#     [<jln-option> <value>]...
#     [DISABLE_OTHERS {on|off}]
#     [BUILD_TYPE type [jln-option> <value>]...]...
# )
jln_flags(CXX_VAR CXX_FLAGS LINK_VAR LINK_FLAGS WARNINGS very_strict)

target_link_libraries(mytarget2 INTERFACE ${LINK_FLAGS})
target_compile_options(mytarget2 INTERFACE ${CXX_FLAGS})

# NOTE: for C, jln_ prefix function becomes jln_c_ and CXX_VAR becomes C_VAR

xmake

Copy output/cpp/xmake_options.lua to myproj/cpp/xmake.lua and output/cpp/xmake to myproj/cpp/flags.lua.

-- launch example: xmake f --jln-sanitizers=on

includes'cpp'

-- Registers new command-line options and set default values
jln_cxx_init_options({warnings='very_strict'} --[[, category=string|boolean]])

options = {}
if is_mode('debug') then
  options.str_debug = 'on'
end

-- Create a new rule. Options are added to the current configuration
jln_cxx_rule('jln_debug', options --[[, disable_others = false, imported='cpp.flags']])
add_rules('jln_flags')

target('hello')
  set_kind('binary')
  -- Custom configuration when jln_cxx_rule() is not enough
  on_load(function(target)
    import'cpp.flags'
    -- getoptions(values = {}, disable_others = false, print_compiler = false)
    -- `values`: table. ex: {warnings='on'}
    -- `values` can have 3 additional fields:
    --  - `cxx`: compiler name (otherwise deducted from --cxx and --toolchain)
    --  - `cxx_version` (otherwise deducted from cxx)
    --  - `ld`: linker name
    -- `disable_others`: boolean
    -- `print_compiler`: boolean
    -- return {cxxflags=table, ldflags=table}
    -- Note: with C language, cxxflags, cxx and cxx_version become cflags, cc and cc_version
    local options = flags.getoptions({elide_type='on'})
    for _,opt in ipairs(options.cxxflags) do target:add('cxxflags', opt, {force=true}) end
    for _,opt in ipairs(options.ldflags) do target:add('ldflags', opt, {force=true}) end

    -- or equivalent (return also options)
    flags.setoptions(target, {elide_type='on'})

    -- return the merge of the default values and new value table
    local values = flags.tovalues({elide_type='on'}, --[[disable_others:bool]])
    print(values)
  end)

  add_files('src/*.cpp')

-- NOTE: for C, jln_ and jln_cxx_ prefix function become jln_c_

Meson

Copy output/cpp/meson_options.txt and rename output/cpp/meson to meson_jln_flags/meson.build.

# launch example: meson -Djln_sanitizers=on
# note: `meson --warnlevel=0` implies `--Djln_warnings=off`

project('test', 'cpp')

# default value (without prefix)
# optional
jln_default_flags = {'rtti': 'off'}

# optional
jln_custom_flags = [
  {'rtti': 'off', 'optimization': '3'}, # (0) opti flags
  {'debug': 'on'}, # (1) debug flags
  # { ... } # (2)
  # etc
]

# declare jln_link_flags, jln_cpp_flags, jln_custom_cpp_flags and jln_custom_link_flags
subdir('meson_jln_flags')

my_opti_cpp_flags = jln_custom_cpp_flags[0] # (0) opti flags (see above)
my_opti_link_flags = jln_custom_link_flags[0]
my_debug_cpp_flags = jln_custom_cpp_flags[1] # (1) debug flags (see above)
my_debug_link_flags = jln_custom_link_flags[1]
# my_... = jln_custom_cpp_flags[2] # (2)
# my_... = jln_custom_link_flags[2]
# etc

executable('demo', 'main.cpp', link_args: jln_link_flags, cpp_args: jln_cpp_flags)

# NOTE: for C, jln_ prefix becomes jln_c_

Premake5

-- launch example: premake5 --jln-sanitizers=on

include "output/cpp/premake5"

-- Registers new command-line options and set default values
jln_newoptions({warnings='very_strict'})

    -- getoptions(values = {}, disable_others = false, print_compiler = false)
    -- `values`: table. ex: {warnings='on'}
    -- `values` can have 3 additional fields:
    --  - `cxx`: compiler name (otherwise deducted from --cxx and --toolchain)
    --  - `cxx_version` (otherwise deducted from cxx)
    --  - `ld`: linker name
    -- `disable_others`: boolean
    -- `print_compiler`: boolean
    -- return {cxxflags=table, ldflags=table}
    -- Note: with C language, cxxflags, cxx and cxx_version become cflags, cc and cc_version
    local options = flags.getoptions({elide_type='on'})
    for _,opt in ipairs(options.cxxflags) do target:add('cxxflags', opt, {force=true}) end
    for _,opt in ipairs(options.ldflags) do target:add('ldflags', opt, {force=true}) end

    -- or equivalent (return also options)
    flags.setoptions(target, {elide_type='on'})

    -- return the merge of the default values and new value table
    local values = flags.tovalues({elide_type='on'}, --[[disable_others:bool]])
    print(values)

-- jln_getoptions(values = {}, disable_others = false, print_compiler = false)
-- `values`: table. ex: {warnings='on'}
-- `values` can have 3 additional fields:
--  - `cxx`: compiler name
--  - `cxx_version` (otherwise deducted from cxx)
--  - `ld`: linker name
-- `disable_others`: boolean
-- `print_compiler`: boolean
-- return {buildoptions=table, linkoptions=table}
-- Note: with C language, cxx and cxx_version become cc and cc_version
local mylib_options = jln_getoptions({elide_type='on'})
buildoptions(mylib_options.buildoptions)
linkoptions(mylib_options.linkoptions)

-- or equivalent
jln_setoptions({elide_type='on'})

-- returns the merge of the default values and new value table
local values = jln_tovalues({elide_type='on'}, --[[disable_others:bool]])
print(values)

-- NOTE: for C, jln_ prefix function becomes jln_c_

Bjam/B2 (Boost.Build)

# launch example: bjam -s jln_sanitizers=on

include output/cpp/bjam ;

# rule jln_flags ( properties * )

project name : requirements
  <jln-lto-default>on # enable jln-lto
  <jln-relro-default>on
  <conditional>@jln_flags
: default-build release ;

exe test : test.cpp : <jln-relro-incidental>off # incidental version of <jln-relro>off

# NOTE: for C, jln_flags becomes jln_c_flags

SCons

# launch example: scons jln_sanitizers=on

import jln_options as jln

jln.set_global_flags({'rtti': 'off'})

vars = Variables(None, ARGUMENTS)
jln.add_variables(vars, {'debug':'on'}) # default value of debug to on

# get_flags(variables[, env]) -> {flags=[...], linkflags=[...]}
flags1 = jln.get_flags(vars)
flags2 = jln.get_flags({'debug':'on'})

Bash alias for gcc/clang

The scripts below add 4 aliases with warnings=on, pedantic=on and color=always.

  • gw++ for g++
  • cw++ for clang++
for comp in g++ clang++ ; do
  version=$($comp --version | sed -E '1!d;s/.*([0-9]\.[0-9]\.[0-9]).*/\1/g')
  echo "alias ${comp:0:1}w++='$comp "$(./compiler-options.lua generators/compiler.lua "$comp-$version" warnings pedantic color=always)\'
done >> ~/.bashrc
  • gwcc for gcc
  • cwcc for clang
for comp in gcc clang ; do
  version=$($comp --version | sed -E '1!d;s/.*([0-9]\.[0-9]\.[0-9]).*/\1/g')
  echo "alias ${comp:0:1}wcc='$comp "$(./compiler-options.lua -c generators/compiler.lua "$comp-$version" warnings pedantic color=always)\'
done >> ~/.bashrc

Generators

Usage:

./compiler-options.lua [-h] [-p] [-c] [-o outfilebase]
                       [-t [-]{platform|compiler|linker}=name[,...]]
                       [-f [-]{option_name[=value_name][,...]}]
                       [-d option_name=value_name[,...]]
                       {generator} [options...]
./compiler-options.lua -f debug,warning generators/cmake.lua # only with debug and warning
./compiler-options.lua -f -debug,warning generators/cmake.lua # without debug nor warning
  • -p Print an AST.
  • -c for C, default is C++.
  • -t Restrict to a list of platform, compiler or linker. When the list is prefixed with '-', values are removed from current AST.
  • -f Restrict to a list of option/value. When the list is prefixed with -, options/values are removed.
  • -d Set new default value. An empty string for value_name is equivalent to default.

generators/compiler.lua

See ./compiler-options.lua generators/compiler.lua -h for detailed usage information.

generators/list_options.lua

$ ./compiler-options.lua generators/list_options.lua [--profile] [--color]

Checks and displays options and their values.

generators/{cmake,xmake,meson,premake5,bjam,scons}.lua

Generators for different build system.

$ ./compiler-options.lua [-o filebase] {generator} [option-prefix]

How to add options?

Edit compiler-options.lua file.

The variable G contains the options tree.

_koptions of Vbase contains the list of available options.

Update the options tree

  • c, cxx, flag, link, fl
c'-Wall' -- C only
cxx'-Wall' -- C++ only
flag'-Wall' -- C and C++
link'-option'
link'libname' -- alias of link'-llibname'
fl'xxx' -- is a alias of {flag'xxx',link'xxx'}

The following functions return the metatable if_mt:

  • gcc, clang, clang_cl, clang_like, msvc, mingw, icc, icx
  • linux, windows, macos
  • vers
gcc { ... } -- for gcc only.
gcc(5) { ... } -- for >= gcc-5
gcc(5, 3) { ... } -- for >= gcc-5.3
gcc(-5, 3) { ... } -- for < gcc-5.3

gcc(major, minor) { ... } -- is a alias of `gcc { vers(major, minor) { ... } }`
  • opt, lvl
opt'warnings' { -- if warnings is enabled (not `warnings=default`)
  lvl'off' { cxx'-w' } -- for `warnings=off`
}
  • Or, And
Or(gcc, clang, msvc) { ... }
And(gcc, lvl'off') { ... }

if_mt

  • -xxx {...} for not xxx
  • xxx {...} / yyy {...} for xxx else yyy
-gcc(5,3) { ... } -- < gcc-5.3
opt'warnings' { -lvl'on' { ... } } -- neither warnings=on nor warnings=default
lvl'on' { xxx } / { yyy } -- equivalent to `{ lvl'on' { xxx }, -lvl'on' { yyy } }`

For a negative form in an Or or an And, it is necessary to make a call without parameter:

And(icc, -windows()) -- And(icc, -windows) not working

Note: -opt'name' isn't allowed

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].