Cmake_args

broken image


Autoware入门教程-源码安装autoware1.12.0说明:介绍如何安装autoware1.12.0步骤:安装系统依赖:$ sudo apt-get update$ sudo apt-g. I think you're mixing up two different paradigms here. As you noted, the highly flexible ExternalProject module runs its commands at build time, so you can't make direct use of Project A's import file since it's only created once Project A has been installed.

Very few applications are fully self-contained, but rather they useexternal libraries and frameworks to do their work. Meson makes itvery easy to find and use external dependencies. Here is how one woulduse the zlib compression library.

First Meson is told to find the external library zlib and error outif it is not found. The version keyword is optional and specifies aversion requirement for the dependency. Then an executable is builtusing the specified dependency. Note how the user does not need tomanually handle compiler or linker flags or deal with any otherminutiae.

If you have multiple dependencies, pass them as an array:

If the dependency is optional, you can tell Meson not to error out ifthe dependency is not found and then do further configuration.

You can pass the opt_dep variable to target construction functionswhether the actual dependency was found or not. Meson will ignorenon-found dependencies.

Meson also allows to get variables that are defined in thepkg-config file. This can be done by using theget_pkgconfig_variable function.

These variables can also be redefined by passing the define_variableparameter, which might be useful in certain situations:

The dependency detector works with all libraries that provide apkg-config file. Art and clothing. Unfortunately several packages don't providepkg-config files. Meson has autodetection support for some of these,and they are described later in thispage.

Note new in 0.51.0new in 0.54.0, the internal keyword

When you need to get an arbitrary variables from a dependency that canbe found multiple ways and you don't want to constrain the type youcan use the generic get_variable method. This currently supportscmake, pkg-config, and config-tool based variables.

It accepts the keywords 'cmake', 'pkgconfig', 'pkgconfig_define','configtool', 'internal', and 'default_value'. 'pkgconfig_define'works just like the 'define_variable' argument toget_pkgconfig_variable. When this method is invoked the keywordcorresponding to the underlying type of the dependency will be used tolook for a variable. If that variable cannot be found or if the callerdoes not provide an argument for the type of dependency, one of thefollowing will happen: If 'default_value' was provided that value willbe returned, if 'default_value' was not provided then an error will beraised.

You can declare your own dependency objects that can be usedinterchangeably with dependency objects obtained from the system. Thesyntax is straightforward:

This declares a dependency that adds the given include directories andstatic library to any target you use it in.

Many platforms do not provide a system package manager. On thesesystems dependencies must be compiled from source. Meson's subprojectsmake it simple to use system dependencies when they are available andto build dependencies manually when they are not.

To make this work, the dependency must have Meson build definitionsand it must declare its own dependency like this:

Then any project that wants to use it can write out the followingdeclaration in their main meson.build file.

What this declaration means is that first Meson tries to look up thedependency from the system (such as by using pkg-config). If it is notavailable, then it builds subproject named foo and from thatextracts a variable foo_dep. That means that the return value ofthis function is either an external or an internal dependency object.Since they can be used interchangeably, the rest of the builddefinitions do not need to care which one it is. Meson will take careof all the work behind the scenes to make this work.

You can use the keyword method to let Meson know what method to usewhen searching for the dependency. The default value is auto.Additional dependencies methods are pkg-config, config-tool, cmake,system, sysconfig, qmake, extraframework and dub.

The dependency method order for auto is:

  1. pkg-config
  2. cmake
  3. extraframework (OSX only)
  4. system

Cmake Args

CMake

Meson can use the CMake find_package() function to detectdependencies with the builtin Find.cmake modules and exportedproject configurations (usually in /usr/lib/cmake). Meson is able touse both the old-style _LIBRARIES variables as well asimported targets.

Extra Cmake_args

It is possible to manually specify a list of CMake targets that shouldbe used with the modules property. However, this step is optionalsince Meson tries to automatically guess the correct target based onthe name of the dependency.

Depending on the dependency it may be necessary to explicitly specifya CMake target with the modules property if Meson is unable to guessit automatically.

Support for adding additional COMPONENTS for the CMakefind_package lookup is provided with the components kwarg(introduced in 0.54.0). All specified componets will be passeddirectly to find_package(COMPONENTS).

Support for packages which require a specified version for CMakefind_package to succeed is provided with the cmake_package_versionkwarg (introduced in 0.57.0). The specified cmake_package_versionwill be passed directly as the second parameter to find_package.

It is also possible to reuse existing Find.cmake files withthe cmake_module_path property. Using this property is equivalent tosetting the CMAKE_MODULE_PATH variable in CMake. The path(s) givento cmake_module_path should all be relative to the project sourcedirectory. Absolute paths should only be used if the CMake files arenot stored in the project itself.

Additional CMake parameters can be specified with the cmake_argsproperty.

Dub

Please understand that Meson is only able to find dependencies thatexist in the local Dub repository. You need to manually fetch andbuild the target dependencies.

For urld.

Other thing you need to keep in mind is that both Meson and Dub needto be using the same compiler. This can be achieved using Dub's-compiler argument and/or manually setting the DC environmentvariable when running Meson.

Cmake Command Line Parameters

Some dependencies have specific detection logic.

Generic dependency names are case-sensitive1,but these dependency names are matched case-insensitively. Therecommended style is to write them in all lower-case.

In some cases, more than one detection method exists, and the methodkeyword may be used to select a detection method to use. The automethod uses any checking mechanisms in whatever order Meson thinks isbest.

e.g. libwmf and CUPS provide both pkg-config and config-tool support.You can force one or another via the method keyword:

Dependencies using config tools

CUPS, LLVM, pcap, WxWidgets,libwmf, GCrypt, GPGME, and GnuStep either do not provide pkg-configmodules or additionally can be detected via a config tool(cups-config, llvm-config, libgcrypt-config, etc). Meson has native support for thesetools, and they can be found like other dependencies:

Since 0.55.0 Meson won't search $PATH any more for a config toolbinary when cross compiling if the config tool did not have an entryin the cross file.

AppleFrameworks

Use the modules keyword to list frameworks required, e.g.

These dependencies can never be found for non-OSX hosts.

System

Some dependencies provide no valid methods for discovery, or do so only insome cases. Some examples of this are Zlib, which provides both pkg-configand cmake, except when it is part of the base OS image (such as in FreeBSDand macOS); OpenGL which has pkg-config on Unices from glvnd or mesa, but hasno pkg-config on macOS and Windows.

In these cases meson provides convenience wrappers in the form of systemdependencies. Internally these dependencies do exactly what a user would doin the build system DSL or with a script, likely callingcompiler.find_library(), setting link_with and include_directories. Byputting these in meson upstream the barrier of using them is lowered, asprojects using meson don't have to re-implement the logic.

Blocks

Enable support for Clang's blocks extension.

(added 0.52.0)

Boost

Boost is not a single dependency but rather a group of differentlibraries. To use Boost headers-only libraries, simply add Boost as adependency.

To link against boost with Meson, simply list which libraries youwould like to use.

You can call dependency multiple times with different modules anduse those to link against your targets.

If your boost headers or libraries are in non-standard locations youcan set the BOOST_ROOT, or the BOOST_INCLUDEDIR andBOOST_LIBRARYDIR environment variables. (added in 0.56.0) You canalso set these parameters as boost_root, boost_include, andboost_librarydir in your native or cross machine file. Note thatmachine file variables are preferred to environment variables, andthat specifying any of these disables system-wide search for boost.

You can set the argument threading to single to use boostlibraries that have been compiled for single-threaded use instead.

CUDA

(added 0.53.0)

Enables compiling and linking against the CUDA Toolkit. The versionand modules keywords may be passed to request the use of a specificCUDA Toolkit version and/or additional CUDA libraries, correspondingly:

Note that explicitly adding this dependency is only necessary if you areusing CUDA Toolkit from a C/C++ file or project, or if you are utilizingadditional toolkit libraries that need to be explicitly linked to.

CUPS

method may be auto, config-tool, pkg-config, cmake or extraframework.

Fortran Coarrays

(added 0.50.0)

Coarrays are a Fortran language intrinsic feature, enabled bydependency('coarray').

GCC will use OpenCoarrays if present to implement coarrays, while Intel and NAGuse internal coarray support.

GL

This finds the OpenGL library in a way appropriate to the platform.

method may be auto, pkg-config or system.

GTest and GMock

GTest and GMock come as sources that must be compiled as part of yourproject. With Meson you don't have to care about the details, justpass gtest or gmock to dependency and it will do everything foryou. If you want to use GMock, it is recommended to use GTest as well,as getting it to work standalone is tricky.

You can set the main keyword argument to true to use the main()function provided by GTest:

HDF5

(added 0.50.0)

HDF5 is supported for C, C++ and Fortran. Because dependencies arelanguage-specific, you must specify the requested language using thelanguage keyword argument, i.e.,

  • dependency('hdf5', language: 'c') for the C HDF5 headers and libraries
  • dependency('hdf5', language: 'cpp') for the C++ HDF5 headers and libraries
  • dependency('hdf5', language: 'fortran') for the Fortran HDF5 headers and libraries

Meson uses pkg-config to find HDF5. The standard low-level HDF5function and the HL high-level HDF5 functions are linked for eachlanguage.

method may be auto, config-tool or pkg-config.

New in 0.56.0 the config-tool method.New in 0.56.0 the dependencies now return proper dependency typesand get_variable and similar methods should work as expected.

libwmf

(added 0.44.0)

method may be auto, config-tool or pkg-config.

LLVM

Meson has native support for LLVM going back to version LLVM version3.5. It supports a few additional features compared to otherconfig-tool based dependencies.

As of 0.44.0 Meson supports the static keyword argument for LLVM.Before this LLVM >= 3.9 would always dynamically link, while olderversions would statically link, due to a quirk in llvm-config.

method may be auto, config-tool, or cmake.

Modules, a.k.a. Components

Meson wraps LLVM's concept of components in it's own modules concept.When you need specific components you add them as modules as Mesonwill do the right thing:

As of 0.44.0 it can also take optional modules (these will affect the argumentsgenerated for a static link):

Using LLVM tools

When using LLVM as library but also needing its tools, it is oftenbeneficial to use the same version. This can partially be achievedwith the version argument of find_program(). However,distributions tend to package different LLVM versions in ratherdifferent ways. Therefore, it is often better to use the llvmdependency directly to retrieve the tools:

MPI

Macro

(added 0.42.0)

MPI is supported for C, C++ and Fortran. Because dependencies arelanguage-specific, you must specify the requested language using thelanguage keyword argument, i.e.,

  • dependency('mpi', language: 'c') for the C MPI headers and libraries
  • dependency('mpi', language: 'cpp') for the C++ MPI headers and libraries
  • dependency('mpi', language: 'fortran') for the Fortran MPI headers and libraries

Meson prefers pkg-config for MPI, but if your MPI implementation doesnot provide them, it will search for the standard wrapper executables,mpic, mpicxx, mpic++, mpifort, mpif90, mpif77. If theseare not in your path, they can be specified by setting the standardenvironment variables MPICC, MPICXX, MPIFC, MPIF90, orMPIF77, during configuration. It will also try to use the Microsoftimplementation on windows via the system method.

method may be auto, config-tool, pkg-config or system.

New in 0.54.0 The config-tool and system method values. Previousversions would always try pkg-config, then config-tool, then system.

NetCDF

(added 0.50.0)

NetCDF is supported for C, C++ and Fortran. Because NetCDF dependencies arelanguage-specific, you must specify the requested language using thelanguage keyword argument, i.e.,

  • dependency('netcdf', language: 'c') for the C NetCDF headers and libraries
  • dependency('netcdf', language: 'cpp') for the C++ NetCDF headers and libraries
  • dependency('netcdf', language: 'fortran') for the Fortran NetCDF headers and libraries

Meson uses pkg-config to find NetCDF.

OpenMP

(added 0.46.0)

This dependency selects the appropriate compiler flags and/or libraries to usefor OpenMP support.

The language keyword may used.

Cmake Args

pcap

(added 0.42.0)

method may be auto, config-tool or pkg-config.

libgcrypt

(added 0.49.0)

method may be auto, config-tool or pkg-config.

GPGME

(added 0.51.0)

method may be auto, config-tool or pkg-config.

Python3

Python3 is handled specially by Meson:

  1. Meson tries to use pkg-config.
  2. If pkg-config fails Meson uses a fallback:
    • On Windows the fallback is the current python3 interpreter.
    • On OSX the fallback is a framework dependency from /Library/Frameworks.

Note that python3 found by this dependency might differ from the oneused in python3 module because modules uses the current interpreter,but dependency tries pkg-config first.

method may be auto, extraframework, pkg-config or sysconfig

Qt4 & Qt5

Meson has native Qt support. Its usage is best demonstrated with anexample.

Here we have an UI file created with Qt Designer and one source andheader file each that require preprocessing with the moc tool. Wealso define a resource file to be compiled with rcc. We just have totell Meson which files are which and it will take care of invoking allthe necessary tools in the correct order, which is done with thepreprocess method of the qt5 module. Its output is simply put inthe list of sources for the target. The modules keyword ofdependency works just like it does with Boost. It tells whichsubparts of Qt the program uses.

You can set the main keyword argument to true to use theWinMain() function provided by qtmain static library (this argumentdoes nothing on platforms other than Windows).

Setting the optional private_headers keyword to true adds theprivate header include path of the given module(s) to the compilerflags. (since v0.47.0)

Note using private headers in your project is a bad idea, do so atyour own risk.

method may be auto, pkg-config or qmake.

SDL2

SDL2 can be located using pkg-confg, the sdl2-config config tool,or as an OSX framework.

method may be auto, config-tool, extraframework orpkg-config.

Threads

Fine art fashion design. This dependency selects the appropriate compiler flags and/orlibraries to use for thread support.

See threads.

Valgrind

Meson will find valgrind using pkg-config, but only uses thecompilation flags and avoids trying to link with it's non-PIC staticlibs.

Vulkan

(added 0.42.0) Eos shutter count.

Vulkan can be located using pkg-config, or the VULKAN_SDKenvironment variable.

method may be auto, pkg-config or system.

WxWidgets

Similar to Boost, WxWidgets is not a single library but rathera collection of modules. WxWidgets is supported via wx-config.Meson substitutes modules to wx-config invocation, it generates

  • compile_args using wx-config --cxxflags $modules..
  • link_args using wx-config --libs $modules..

Example

Shaderc

Cmake Function Optional Argument

(added 0.51.0)

Shaderc currently does not ship with any means of detection.Nevertheless, Meson can try to detect it using pkg-config, but willdefault to looking for the appropriate library manually. If thestatic keyword argument is true, shaderc_combined is preferred.Otherwise, shaderc_shared is preferred. Note that it is not possibleto obtain the shaderc version using this method.

method may be auto, pkg-config or system.

Zlib

Zlib ships with pkg-config and cmake support, but on some operatingsystems (windows, macOs, FreeBSD, dragonflybsd), it is provided aspart of the base operating system without pkg-config support. The newSystem finder can be used on these OSes to link with the bundledversion.

method may be auto, pkg-config, cmake, or system.

New in 0.54.0 the system method.

Curses

(Since 0.54.0)

Curses (and ncurses) are a cross platform pain in the butt. Mesonwraps up these dependencies in the curses dependency. This coversboth ncurses (preferred) and other curses implementations.

method may be auto, pkg-config, config-tool, or system.

Cmake Args Macro

New in 0.56.0 The config-tool and system methods.

1: They may appear to be case-insensitive, if the underlying file system happens to be case-insensitive.

At this point we have a Windows build and remote Linux build working from Windows in the Visual Studio IDE. This is all good and it's hard to beat VS in terms of productivity while actively developing the code. However, there are still plenty of scenarios where command line is superior (e.g. when setting up build automation).

Unfortunately, it is not quite as easy as running cmake.exe on the command line to get the same experience that VS has provided so far. There are actually three things that are happening when building and testing the project in VS:

  1. Producing native build system files via a CMake generator
  2. Executing the native build step
  3. Discovering and executing the tests

The first step is where CMake comes in but it must be properly initialized. This requires understanding which generator and make program to use as well as some of the required paths. By observing how VS launches CMake (Sysinternals Process Monitor FTW!), I was able to construct a command line that has identical behavior. It looks something like this:

cmake.exe -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER:FILEPATH=cl.exe -DCMAKE_INSTALL_PREFIX:PATH='PROJPATHoutinstallx64-Debug' -DCMAKE_MAKE_PROGRAM=ninja.exe -DCMAKE_TOOLCHAIN_FILE='VCPKG/PATH/scripts/buildsystems/vcpkg.cmake' 'PROJPATH'

There are some paths to fill in based on the particular system and project, but this gives you a general idea. Also note that this command must be executed in the build output path, e.g. outbuildx64-Debug.

Given that VS uses the Ninja generator to produce build system files, we would need to invoke ninja.exe. In this case, it is just a simple matter of launching the EXE — any other arguments are optional and would only be needed if you wanted to change output logging behavior, for example.

Testing is also just as easy. Since CMake and Ninja have already done all the hard work, we would launch ctest.exe to execute tests.

Given the tedium of trying to stitch together all the required paths and setting up the right folders, these steps are just begging to be automated. So here is a batch file build.cmd which does just that:

Note the additional functionality which can be enabled via command line options: --clean to delete the previous output folder before starting, --no-test to skip the testing step, and --verbose to pass verbose flags to each of the tools. The default build target is Debug, or you can say build Release for, you guessed it, the release build. Since this assumes the same development environment as Visual Studio, it should be run from inside a VS developer command prompt.

Running it for the first time or on any changes will generate all the binaries and run tests. Running it without any changes should quickly blaze past the generation/compilation steps and just run tests:

If Windows were our only concern, we would be done here. But I promised cross-platform, so we need to figure out the equivalent steps in a Linux environment. Luckily for us, in this case, the steps are very similar. We just need a Linux-native way to perform them, which means it's time for Bash! Here is build.sh, a relatively faithful port of the above script using the all-around superior Bash constructs (conditionals, looping, functions, etc.):

You might notice that the cmake arguments are slightly different here. For Linux, we don't really need vcpkg and we choose the CLang compiler since MSVC (cl.exe) wouldn't make sense. I also found that it has trouble with quoted paths so I omitted them. Anyway, just as we had hoped, the Linux behavior is equivalent to Windows when launching the script from an already produced Release build:

Here is the GitHub repo with the changes: CMakeSampleVS

Amazingly, we have achieved our original goals. We have a project structure which works with the VS IDE on Windows, produces platform-specific binaries for Windows and Linux, supports a command line build for both Windows and Linux, and has full unit test support on both platforms. What kinds of modern C++ software could we create with these newfound capabilities?





broken image