ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.13k stars 439 forks source link

some tool-chains might not support -march=native or -mtune=native #507

Closed SSE4 closed 7 years ago

SSE4 commented 7 years ago

some tool-chains (e.g. Android NDK) might not support -march=native or -mtune=native compiler flags. for example, gcc throws the following error:

cc1plus: error: unrecognized argument in option '-march=native'
cc1plus: note: valid arguments to '-march=' are: armv2 armv2a armv3 armv3m armv4 armv4t armv5 armv5e armv5t armv5te armv6 armv6-m armv6j armv6k armv6s-m armv6t2 armv6z armv6zk armv7 armv7-a armv7-m armv7-r armv7e-m armv7ve armv8-a armv8-a+crc iwmmxt iwmmxt2 native
cc1plus: error: unrecognized argument in option '-mtune=native'
cc1plus: note: valid arguments to '-mtune=' are: arm1020e arm1020t arm1022e arm1026ej-s arm10e arm10tdmi arm1136j-s arm1136jf-s arm1156t2-s arm1156t2f-s arm1176jz-s arm1176jzf-s arm2 arm250 arm3 arm6 arm60 arm600 arm610 arm620 arm7 arm70 arm700 arm700i arm710 arm7100 arm710c arm710t arm720 arm720t arm740t arm7500 arm7500fe arm7d arm7di arm7dm arm7dmi arm7m arm7tdmi arm7tdmi-s arm8 arm810 arm9 arm920 arm920t arm922t arm926ej-s arm940t arm946e-s arm966e-s arm968e-s arm9e arm9tdmi cortex-a12 cortex-a15 cortex-a15.cortex-a7 cortex-a5 cortex-a53 cortex-a57 cortex-a57.cortex-a53 cortex-a7 cortex-a8 cortex-a9 cortex-m0 cortex-m0plus cortex-m1 cortex-m3 cortex-m4 cortex-r4 cortex-r4f cortex-r5 cortex-r7 ep9312 fa526 fa606te fa626 fa626te fa726te fmp626 generic-armv7-a iwmmxt iwmmxt2 marvell-pj4 mpcore mpcorenovfp native strongarm strongarm110 strongarm1100 strongarm1110 xscale
CMakeFiles/test.header.meta.meta.hpp.dir/build.make:54: recipe for target 'CMakeFiles/test.header.meta.meta.hpp.dir/headers/meta/meta.cpp.o' failed
make[2]: *** [CMakeFiles/test.header.meta.meta.hpp.dir/headers/meta/meta.cpp.o] Error 1
CMakeFiles/Makefile2:928: recipe for target 'CMakeFiles/test.header.meta.meta.hpp.dir/all' failed
make[1]: *** [CMakeFiles/test.header.meta.meta.hpp.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2

and clang:

clang38++: error: the clang compiler does not support '-march=native'
CMakeFiles/test.header.meta.meta.hpp.dir/build.make:54: recipe for target 'CMakeFiles/test.header.meta.meta.hpp.dir/headers/meta/meta.cpp.o' failed
make[2]: *** [CMakeFiles/test.header.meta.meta.hpp.dir/headers/meta/meta.cpp.o] Error 1
CMakeFiles/Makefile2:928: recipe for target 'CMakeFiles/test.header.meta.meta.hpp.dir/all' failed
make[1]: *** [CMakeFiles/test.header.meta.meta.hpp.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
/host/Git/test/range-v3
gnzlbg commented 7 years ago

cc1plus: error: unrecognized argument in option '-march=native'

cc1plus: note: valid arguments to '-march=' are: armv2 [...] native

I am puzzled. It says that it doesn't recognize the argument but that it is still a valid argument?

We could use:

include(CheckCXXCompilerFlag)
macro(range_v3_append_flag testname flag)
    check_cxx_compiler_flag(${flag} ${testname})
    if (${testname})
        add_compile_options(${flag})
    endif()
endmacro()
range_v3_append_flag(RANGE_V3_HAS_MARCH_NATIVE "-march=native")
range_v3_append_flag(RANGE_V3_HAS_MTUNE_NATIVE "-mtune=native")

to detect whether the compiler supports a particular flag, and then use it only if supported (i.e. if compiling a dummy file with the flag doesn't produce a compilation error).