Popular languages such as C, C++, python, ksh are always case-sensitive for both variables and functions. So, I'm surprised to learn that CMake treats that differently. What's the reason? Thank you.
CMake variables are case-sensitive, as in other popular languages. However, the CMake commands are case-insensitive for historical reasons, and due to CMake's strict adherence to backwards-compatibility. A quote from CMake maintainer, Brad King:
Ancient CMake versions required upper-case commands. Later command
names became case-insensitive. Now the preferred style is lower-case.
indicates that CMake commands used to be upper-case. However, the convention now is to use lower-case commands.
Many behaviors and conventions from older versions of CMake have been maintained (and aged-out) using CMake policies. The cmake_policy command gives developers control over whether the old or new behavior should be used. However, as far as I know, there is no policy dictating case-sensitivity for CMake commands.
Related
I am working on fixing some CMake files that have been made with a version newer than 3.10, but should have been coded to be compatible with 3.10.
There is a particular line:
add_compile_definitions(SOME_BOOL_VARIABLE)
that I can not figure out how to change to make it work with version 3.10. The add_definitions(SOME_BOOL_VARIABLE) command was the obvious thing that I tried, but produces errors with "no such file of directory".
From the CMake documentation, the functionality of add_definitions() and add_compile_definitions() is essentially the identical:
Adds preprocessor definitions to the compiler command line for targets in the current directory and below (whether added before or after this command is invoked).
The difference is in the syntax accepted for the command arguments. The older add_definitions() command requires the -D flag be prepended to each definition.
add_definitions(-DSOME_BOOL_VARIABLE)
The newer add_compile_definitions() command (available in CMake 3.12 and above) is cleaner, and does not require the -D flag prefix.
If you are refactoring your code, modern CMake encourages a target-centric approach. Whenever possible, you should prefer the target_compile_definitions() command to add preprocessor definitions to only those targets that require them.
target_compile_definitions(MyLibraryTarget PRIVATE SOME_BOOL_VARIABLE)
In CMake, there is a tendency for many library-finding commands and CMake defaults to fill up your build commands with garbage that is totally irrelevant to a given target.
How can you blow away all of the flags/properties/settings that determine the compile options for a target so you can set just the ones you want?
I'm using cmake 2.8 since that's what's in Ubuntu 14.04, but hopefully there is some version that works across several cmake versions, despite the infuriatingly frequent renames of cmake variables and functions to subtly different names and subtly different behaviors.
At the moment I'm building a shared library from a single .c file, but I think my question applies in general for C and C++ libraries and executables.
In the 2.x version of CMake, the cmake option CMAKE_BUILD_TYPE controlled options to be passed to the compiler. For example, if CMAKE_BUILD_TYPE=RelWithDebInfo then the options passed to the compiler was CMAKE_CXX_FLAGS_RELWITHDEBINFO for C++ and CMAKE_C_FLAGS_RELWITHDEBINFO for C (http://binglongx.wordpress.com/tag/cmake_build_type/).
In the new 3.x version of CMake there are commands add_compile_options and target_compiler_options for adding options to the compiler (What is the modern method for setting general compile flags in CMake?).
Questions
How do I define which build type CMake should use? Is it still, for example, cmake -DCMAKE_BUILD_TYPE=Debug?
How do I make use of this build type in my CMakeLists.txt?
Has CMake defined all the build types, or can I define custom, additional, build types?
Has CMake set any default compiler options for each of the build types (for example -g for the Debug build type)?
Please try to stick to a single question per question in the future. You might risk getting closed as too broad otherwise. Also, you are more likely to get good answers for well-defined, precise questions.
How do I define which build type CMake should use? Is it still, for
example, cmake -DCMAKE_BUILD_TYPE=Debug?
Yes, this did not change.
How do I make use of this build type in my CMakeLists.txt?
Use generator expressions. For example:
target_compile_options(my_program PUBLIC $<$<CONFIG:Debug>:-Werror>)
# Warnings are errors for debug build only
Has CMake defined all the build types, or can I define custom,
additional, build types?
You can add your own type if the defaults don't cut it for you. Note though that this can be a bit fiddly, so I wouldn't do it unless you have good reasons. Take a look at CMAKE_CONFIGURATION_TYPES.
Has CMake set any default compiler options for each of the build types
(for example -g for the Debug build type)?
CMake does a pretty good job at choosing the right defaults for the different configurations. In particular, Debug and RelWithDebInfo builds generate symbols correctly (-g on gcc) and Release builds are optimized quite well.
I'm trying to get rid of cmake in my project for some reasons. I need to create unix makefiles to build my project. If i use cmake to generate them for me, that makefiles would depend of cmake anyway. The only build tools that I can use is one from GNU toolchain.
cmake was invented to be a portable way to create makefiles. If you want to have a look at an alternative, take a look at bjam from boost. This tool works with a lot of toolchains (called toolset in the bjam terminology) and is quite simple to use as cmake is.
If you really want to get rid of cmake or bjam, then write your own makefiles taking the ones generated by cmake as a base for example... But this will limit the scope of systems and toolchains on which your code will compile. To be honest I would see rather that as a pain and encourage you to use bjam if you need better support for other toolsets.
I'm just found cmake and I want to use it to create make files for a little project that uses the esql compiler.
I've not used cmake yet (it is on my list of things that I need to look at - round about the time some spare tuits become available), but...
I do have several sets of rules for compiling ESQL/C to object code etc for regular make.
You can find one set of those rules online at the IIUG Software Archive in the SQLCMD package. Or you can contact me directly to discuss the niceties in detail (and/or the differences between cmake stuff and regular make stuff). You can also find Informix-related autoconf macros in the SQLCMD package - file acinformix.m4.
You will probably need to use the cmake ADD_CUSTOM_COMMAND command to create the rule for compiling each source file with the esql compiler.