[CMake] Is there a way to obtain the current compiler standard flag?

Craig Scott craig.scott at crascit.com
Fri Jun 1 17:42:26 EDT 2018


On Sat, Jun 2, 2018 at 6:32 AM, Chris Green <greenc at fnal.gov> wrote:

> Ack! Apparently even the hard way won't work, because apparently one
> cannot nest $<IF clauses, viz:
>
>  set(CXX_STD_FLAG "$<IF:$<TARGET_PROPERTY:${dictname}_dict,CXX_EXTENSIONS>,\
> $<IF:$<EQUAL:11,$<TARGET_PROPERTY:${dictname}_dict,CXX_STANDARD>>,${CMAKE_CXX11_EXTENSION_COMPILE_OPTION},\
> $<IF:$<EQUAL:14,$<TARGET_PROPERTY:${dictname}_dict,CXX_STANDARD>>,${CMAKE_CXX14_EXTENSION_COMPILE_OPTION},\
> $<IF:$<EQUAL:17,$<TARGET_PROPERTY:${dictname}_dict,CXX_STANDARD>>,${CMAKE_CXX17_EXTENSION_COMPILE_OPTION},\
> $<IF:$<EQUAL:20,$<TARGET_PROPERTY:${dictname}_dict,CXX_STANDARD>>,${CMAKE_CXX20_EXTENSION_COMPILE_OPTION},\
> ${CMAKE_CXX98_EXTENSION_COMPILE_OPTION}>>>>,\
> $<IF:$<EQUAL:11,$<TARGET_PROPERTY:${dictname}_dict,CXX_STANDARD>>,${CMAKE_CXX11_STANDARD_COMPILE_OPTION},\
> $<IF:$<EQUAL:14,$<TARGET_PROPERTY:${dictname}_dict,CXX_STANDARD>>,${CMAKE_CXX14_STANDARD_COMPILE_OPTION},\
> $<IF:$<EQUAL:17,$<TARGET_PROPERTY:${dictname}_dict,CXX_STANDARD>>,${CMAKE_CXX17_STANDARD_COMPILE_OPTION},\
> $<IF:$<EQUAL:20,$<TARGET_PROPERTY:${dictname}_dict,CXX_STANDARD>>,${CMAKE_CXX20_STANDARD_COMPILE_OPTION},\
> ${CMAKE_CXX98_STANDARD_COMPILE_OPTION}>>>>>")
>
> begets:
>
>   First parameter to $<IF> must resolve to exactly one '0' or '1' value.
>
> I'm all out of ideas. Any new ones would be appreciated.
>

The very first line of that is assuming the CXX_EXTENSIONS target property
resolves to 0 or 1, but it could be other boolean constants like YES, NO,
TRUE, FALSE, etc. Wrap it in $<BOOL:...> to make it robust:

set(CXX_STD_FLAG
"$<IF:$<BOOL:$<TARGET_PROPERTY:${dictname}_dict,CXX_EXTENSIONS>>,\






> On 6/1/18 3:00 PM, Chris Green wrote:
>
> Hi,
>
> I was hoping this would work:
>
> $<IF:$<TARGET_PROPERTY:CXX_EXTENSIONS>,${CMAKE_CXX$<TARGET_PROPERTY:CXX_STANDARD>_EXTENSION_COMPILE_OPTION},${CMAKE_CXX$<TARGET_PROPERTY:CXX_STANDARD>_STANDARD_COMPILE_OPTION}>
>
> But apparently it's not legal to calculate a variable name based on a
> generator expression. Is there some elegant way to get want I want here or
> do I have to check specifically for 98, 11, 14, 17, 20, etc?
>
> Thanks,
>
> Chris.
>
> On 6/1/18 8:27 AM, Chris Green wrote:
>
> Hi,
>
> I'll give this a shot, thank you. Ideally we'd have a generator expression
> pick up ${std} from the relevant target property, but I'll have to play
> with that since I'm not familiar enough with generator expressions to know *a
> priori* how this will turn out.
>
> Thanks for your help,
>
> Chris.
>
> On 5/30/18 3:12 PM, Chuck Atkins wrote:
>
> Hi Chris.
>
> Try using the the CMAKE_CXX${std}_STANDARD_COMPILE_OPTION variable.  For
> example, the folowing piece of CMake code:
> cmake_minimum_required(VERSION 3.9)
>
> project(foo CXX)
>
> foreach(std IN ITEMS 98 11 14 17)
>   message("C++${std} std flags: ${CMAKE_CXX${std}_STANDARD_
> COMPILE_OPTION}")
>   message("C++${std} ext flags: ${CMAKE_CXX${std}_EXTENSION_
> COMPILE_OPTION}")
> endforeach()
>
> Wll generate for GCC on Linux:
> -- The CXX compiler identification is GNU 8.1.1
> -- Check for working CXX compiler: /usr/bin/c++
> -- Check for working CXX compiler: /usr/bin/c++ -- works
> -- Detecting CXX compiler ABI info
> -- Detecting CXX compiler ABI info - done
> -- Detecting CXX compile features
> -- Detecting CXX compile features - done
> C++98 std flags: -std=c++98
> C++98 ext flags: -std=gnu++98
> C++11 std flags: -std=c++11
> C++11 ext flags: -std=gnu++11
> C++14 std flags: -std=c++14
> C++14 ext flags: -std=gnu++14
> C++17 std flags: -std=c++1z
> C++17 ext flags: -std=gnu++1z
> -- Configuring done
>
> PGI on Linux:
> -- The CXX compiler identification is PGI 18.4.0
> -- Check for working CXX compiler: /opt/pgi/linux86-64/18.4/bin/pgc++
> -- Check for working CXX compiler: /opt/pgi/linux86-64/18.4/bin/pgc++ --
> works
> -- Detecting CXX compiler ABI info
> -- Detecting CXX compiler ABI info - done
> -- Detecting CXX compile features
> -- Detecting CXX compile features - done
> C++98 std flags: -A
> C++98 ext flags: --gnu_extensions
> C++11 std flags: --c++11;-A
> C++11 ext flags: --c++11;--gnu_extensions
> C++14 std flags: --c++14;-A
> C++14 ext flags: --c++14;--gnu_extensions
> C++17 std flags: --c++17;-A
> C++17 ext flags: --c++17;--gnu_extensions
> -- Configuring done
>
>
> And for IBM XL on AIX:
> -- The CXX compiler identification is XL 13.1.3
> -- Check for working CXX compiler: /usr/bin/xlC
> -- Check for working CXX compiler: /usr/bin/xlC -- works
> -- Detecting CXX compiler ABI info
> -- Detecting CXX compiler ABI info - done
> -- Detecting CXX compile features
> -- Detecting CXX compile features - done
> C++98 std flags: -qlanglvl=strict98
> C++98 ext flags: -qlanglvl=extended
> C++11 std flags: -qlanglvl=extended0x
> C++11 ext flags: -qlanglvl=extended0x
> C++14 std flags:
> C++14 ext flags:
> C++17 std flags:
> C++17 ext flags:
> -- Configuring done
> -- Generating done
>
> - Chuck
>
>
>


-- 
Craig Scott
Melbourne, Australia
https://crascit.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180602/176e0901/attachment.html>


More information about the CMake mailing list