[CMake] How to do platform specific setup?

Robert Dailey rcdailey.lists at gmail.com
Wed Jul 26 12:14:29 EDT 2017


According to Brad King, toolchain files are not intended for what I'm
asking about. I recall someone mentioning some built-in modules in
CMake that are responsible for toolchain and platform setup, that can
be overridden somehow. This is different from toolchain files AFAIK. I
apologize for being vague, but I was hoping someone would recognize
what I'm talking about.

On Wed, Jul 26, 2017 at 10:29 AM, Mario Werner
<mario.werner at iaik.tugraz.at> wrote:
> Hi Robert,
>
> I usually do all my platform and compiler specific customization in a
> toolchain file [1]. Such a toolchain file is more or less standard CMake
> script which get executed before the root CMakeLists.txt from the
> project is processed. When invoking cmake for the first time you simply
> specify which toolchain file you want to use by adding
> `-DCMAKE_TOOLCHAIN_FILE=<path-to-file>` as parameter.
>
> Note that the idea of a toolchain file is that the file is customized
> exactly for your environment.
>
> Best regards,
> Mario
>
>
> [1] https://cmake.org/Wiki/CMake_Cross_Compiling#The_toolchain_file
>
> On 2017-07-19 21:22, Robert Dailey wrote:
>> So in one of my top level CMake scripts, I have this:
>>
>> ```
>> if( ANDROID )
>>     include( android )
>>     _setup_android_platform()
>> elseif( UNIX )
>>     include( unix )
>>     _setup_unix_platform()
>> endif()
>>
>> if( CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
>>     include( clang )
>>     _setup_clang_toolchain()
>> elseif( CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" )
>>     include( msvc )
>>     _setup_msvc_toolchain()
>> endif()
>> ```
>>
>> This allows me to set up various global things used by all targets
>> defined by CMake. An example of what android's does:
>>
>> ```
>> macro( _setup_android_platform )
>>     # Forward the android ABI from CMake to a custom variable for
>> portability to other toolchains
>>     if( CMAKE_ANDROID_ARCH_ABI )
>>         set( ANDROID_ABI ${CMAKE_ANDROID_ARCH_ABI} )
>>     endif()
>>
>>     # Forcefully disable testing while cross compiling for Android
>> platform. It doesn't make sense
>>     # to build tests, since we can't run them natively. We build tests
>> on a separate platform.
>>     set( BUILD_TESTING OFF CACHE BOOL "Do not build unit tests when
>> cross compiling for Android" FORCE )
>>
>>     android_ndk_import_module_cpufeatures()
>>     android_ndk_import_module_native_app_glue()
>>
>>     # Export ANativeActivity_onCreate(),
>>     # Refer to: https://github.com/android-ndk/ndk/issues/381.
>>     set( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u
>> ANativeActivity_onCreate" )
>>
>>     add_definitions(
>>         -DANDROID
>>     )
>> endmacro()
>> ```
>>
>> And clang:
>>
>> ```
>> macro( _setup_clang_toolchain )
>>     # Disable certain clang warnings
>>     # These spam like crazy; disable for now
>>     add_compile_options(
>>         -Wno-inconsistent-missing-override
>>     )
>>
>>     # Clang 3.8.0 on Ubuntu was failing with -Wno-expansion-to-defined.
>>     # Version 3.8.2 in NDK r14b seems ok with it.
>>     if( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 3.8.2 )
>>         add_compile_options(
>>             -Wno-expansion-to-defined
>>         )
>>     endif()
>>
>>     set( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}
>> -Wl,--no-undefined" )
>> endmacro()
>> ```
>>
>> I feel like there is a better way. I recall there being some built-in
>> way to override CMake's platform specific setup modules, but I don't
>> know anything about it. Can someone offer some advice on if there is a
>> built in way to do this?
>>
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake


More information about the CMake mailing list