[CMake] coding style for modules

Stephen Orso stephen.orso at outlook.com
Fri Jan 5 15:40:59 EST 2018


On Fri, 5 Jan 2018 18:16:08 +0300 Dave Milter <davemilter at gmail.com> wrote:
> b)Include this project as git submodule and use ExternalProject to
>>> invoke `cmake` and `cmake --build`
>> This would be a clean solution if you want to build libfoo as part of yur
>> project.
>>
> Thank you for answer, but how handle cross compilation in this case?
> For example I invoke `cmake
> -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake`
> for master project.
> Does `cmake` automatically add proper ` -DCMAKE_TOOLCHAIN_FILE=`
> option into `cmake` invocation in ExternalProject?
I haven't gotten to cross compilation yet, but my CMake project uses
externalproject_add for several modules.  I want them built using the
same generator and the same configuration type as the parent (consuming)
project.  Some of the allowed generators for this project are 
multi-configuration,
and some are not.

I use the following CMake code:

<CMake code>

# WINTARGET is needed by the configure-time external package build
# and for the creation of the externalproject_add() target.
if( NOT ${WINTARGET} STREQUAL "" )
     set( __wintarget -DWINTARGET=${WINTARGET} )
endif( )

# If we are configuring using a single-configuration generator,
# the configuration name is needed by the configure-time external
# package build and for the creation of the externalproject_add()
# target.
if( "${CMAKE_CONFIGURATION_TYPES}" STREQUAL "" )
     set( __build_type "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" )
endif( )

if( NOT "${CMAKE_GENERATOR_PLATFORM}" STREQUAL "" )
     set(__platform -A ${CMAKE_GENERATOR_PLATFORM} )
endif( )
if( NOT "${CMAKE_GENERATOR_TOOLSET}" STREQUAL "" )
     set(__toolset -T ${CMAKE_GENERATOR_TOOLSET} )
endif( )

# When building a static library using externalproject_add() and
# using Ninja for the build tool, we must include the library name
# in the BUILD BYPRODUCTS option of externalproject_add(). See the
# discussion at https://cmake.org/pipermail/cmake/2015-April/060233.html.
# This issue applies only to SoftFloat-3a at the moment because it
# is the only static library built and used by this project.

# Retrieve the full path name of the static library for the current
# configuration from the imported target.  This is relatively simple
# because Ninja is a single-configuration build tool, so we know
# which configuration should be retrieved from the target at
# configure time.

if( "${CMAKE_GENERATOR}" STREQUAL "Ninja" )
     if( "${pkgid}" MATCHES "^[Ss]3[Ff][Hh]$" )
         string( TOUPPER "${CMAKE_BUILD_TYPE}" __build_byproducts )
         get_target_property(
                 __build_byproducts
                 SoftFloat
                 IMPORTED_LOCATION_${__build_byproducts}
                 )
         message( "Build type ${CMAKE_BUILD_TYPE}, Setting 
BUILD_BYPRODUCTS to ${__build_byproducts}" )
     endif( )
endif( )

# externalproject_add() does not expose a git depth option, so we
# must download the entire repository.  Oh well...maybe someday.
# Note that "<INSTALL_DIR>" below is not a typo'd generator
# expression.

externalproject_add( ${pkg}
         PREFIX            ${__pkg_prefix}
         SOURCE_DIR        ${__pkg_prefix}/pkgsrc
         BINARY_DIR        ${__pkg_prefix}/build
         INSTALL_DIR       ${__pkg_prefix}/install
         GIT_REPOSITORY    ${pkgurl}
         GIT_TAG           ${pkgbranch}
         CMAKE_GENERATOR           ${CMAKE_GENERATOR}
         CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET}
         CMAKE_GENERATOR_PLATFORM ${CMAKE_GENERATOR_PLATFORM}
         CMAKE_ARGS
                 ${__wintarget}
                 ${__build_type}
-D${__CMAKE}INSTALL_PREFIX=<INSTALL_DIR>
                 -DCMAKE_REQUIRED_QUIET=TRUE
         BUILD_BYPRODUCTS  ${__build_byproducts}
         PATCH_COMMAND     ""        # No patches
         UPDATE_COMMAND    ""        # No updates
         INSTALL_COMMAND   ""        # ..and no install.
     )

</CMake code>


I expect it would not be difficult to add the toolchain file in the same 
way.

-- 
Best Regards,
Steve Orso



More information about the CMake mailing list