How to handle conditional cflags with meson? - build

I'm new to meson, just looking through the documentation at this point. What is your recommended way of handling conditional cflags in a meson build?
Say for Visual Studio I want to use /DNOMINMAX and a suppress a bunch of warnings only VS produces like /wd4626 /wd4640, etc. How would you suggest doing this?

I solved this using a directory hierarchy and meson calls to get compiler version.
In my project root directory, I have a subdirectory for configuration stuff called _meson.
_meson/
meson.build
compilers/
meson.build
clang/
meson.build
7.0/
meson.build
gcc/
meson.build
4.9/
meson.build
msvc/
meson.build
vs2015/
meson.build
vs2010/
meson.build
The root meson.build calls subdir('_meson') to include the configurations.
The _meson/meson.build calls subdir('compilers') to pull in configuration about compilers - the extra layer is in case I have platform specific configurations to add later.
Then _meson/compilers/meson.build looks like:
compiler_info = meson.get_compiler('cpp')
compiler = compiler_info.get_id()
compiler_version = compiler_info.version().split('-').get(0)
# setup some variables for compiler configurations
compiler_version_major = compiler_version.split('.').get(0)
compiler_version_minor = compiler_version.split('.').get(1)
compiler_version_build = compiler_version.split('.').get(2)
# load compiler configurations
subdir(compiler)
The last line directs meson to include the subdirectory with the same name as the compiler. So when compiling with gcc, this would evaluate to subdir('gcc').
This meson.build is where configuration information for all versions of that compiler would go. This file might look like:
# Put config information here...
# Include version specific configuration
subdir(compiler_version_major + '.' + compiler_version_minor)
The subdirectories under the compiler name would contain configuration information for specific compiler versions, should they need some flags/config not all versions of the compiler support.
If possible, it would be helpful to make the subdir() conditional on the existence of the directory so the build doesn't break when building with a different specific build of the compiler.
Caveat: I don't have meson running on windows yet, so I can't be sure the version string has 3 parts like it does for clang and gcc.

Related

Is there "includePath" option in clangd?

I used to work with VSCode C/C++ extension. there was a feature in this extension(in a json file), called "includePath", which I could set the paths for my headers, so without execution of CMake or make, I would have the suggestion of my headers and code completion from those.
now I have switched to neovim and clangd as the language server for code completion. I searched a lot to find the corresponding feature in clangd options but I could not find anything more than this link.
since the clangd is a powerful language server, I am in wonder if there is not such a feature in it. so I want to know is there actually such a feature in clangd? and if YES how can I use that?
Note: I use a language client, called "coc-clangd". I don't know if it matters or not.
Clangd uses compile_commands.json database file which contains flags (such as include directories) for each file in project. But this file is auto-generated, so all modifications to it will be overwritten eventually. You can ask CMake to add any custom compile flags with -DCMAKE_CXX_FLAGS command line argument.
Example for system headers (#include <file.h>):
cmake -DCMAKE_CXX_FLAGS="-isystem /path/to/includes" /path/to/source
For project headers (#include "file.h"):
cmake -DCMAKE_CXX_FLAGS=-Ipath/to/includes /path/to/source
Additionally, you can set CXXFLAGS environment variable:
export CXXFLAGS="-isystem /path/to/includes"
cmake path/to/sources
After that new flags should appear in your compile_commands.json file.
Maybe this is useful: https://clangd.llvm.org/config
Create a file called '.clangd' in the top-level of the source directory.
Add those content.
CompileFlags: # Tweak the parse settings
Add:
- "-I=[folder]"
But I think this is not recommend, all include directories should be add in CMakeLists.txt file.
You can add includePath to clangd.fallbackFlags into vscode's settings.json like this:
"clangd.fallbackFlags": [
"-I${workspaceFolder}/include",
"-I/my/include"
]
To use code completion provided by Clangd, let Clangd retrieve include paths from compiler_commands.json with compiler calls used by CMake. Set the CMAKE_EXPORT_COMPILE_COMMANDS option in CMakeLists.txt, it will output compiler_commands.json to the build directory when CMake is run:
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Copy the generated compiler_commands.json to the project source directory. Clangd will now source this file.

How to set specific CMAKE_C_OUTPUT_EXTENSION for cross-compiling configurations with CMAKE?

I am trying to setup a toolchain file for cross compilation with CMake 3.12.0 version.
My object files have a different extensions than .obj on Windows and .o in UNIX.
Thus, I set my CMAKE_LANG_OUTPUT_EXTENSION to .src.
Unfortunately, this variable is overwritten by CMakeCInformation.cmake file in these lines:
# some compilers use different extensions (e.g. sdcc uses .rel)
# so set the extension here first so it can be overridden by the compiler specific file
if(UNIX)
set(CMAKE_C_OUTPUT_EXTENSION .o)
else()
set(CMAKE_C_OUTPUT_EXTENSION .obj)
endif()
If I comment these lines my configurations will work and the right object extension will be used.
I think my toolchain file is configured so that CMake will not execute its internal compiler checks.
This is how my toolchain file entry lines look:
SET(CMAKE_SYSTEM_NAME Generic)
INCLUDE(CMakeForceCompiler)
SET(CMAKE_C_COMPILER_FORCED TRUE)
SET(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
#other compiler configuration lines here
SET(CMAKE_C_OUTPUT_EXTENSION .src)
SET(CMAKE_ASM_OUTPUT_EXTENSION .o)
SET(CMAKE_C_OUTPUT_EXTENSION_REPLACE 1)
SET(CMAKE_ASM_OUTPUT_EXTENSION_REPLACE 1)
I know CMakeForceCompiler is depreciated and CMAKE_TRY_COMPILE_TARGET_TYPE should be used that is why both are there.
I am telling CMake about my toolchain file using -DCMAKE_TOOLCHAIN_FILE
Can you please help me figure out what I am doing wrong?
EDIT: I was also trying to CACHE the value of CMAKE_C_OUTPUT_EXTENSION. At least for me this didn't work.
Add SET(CMAKE_C_OUTPUT_EXTENSION .src) in the CMakeLists.txt file after the project command not in the toolchain file. This should get you the desired behavior (as it should override the values set by CMakeCInformation and any other module scripts).
The toolchain file is used for setting basic toolchain information on where the compiler is and some basic settings. Other variables need to be setup afterwards by custom compiler or platform files that can be included via CMAKE_USER_MAKE_RULES_OVERRIDE.
From CMake issue 18713: "This issue has been reported before and it was mentioned that the toolchain file isn't where these kinds of things should be set for more complicated toolchains."
CMake issue 398139 "Toolchain files should not be setting things like this. ... The output extension should be specified by compiler or platform information files that are loaded by CMakeCInformation after the defaults here are set."

CMake with Xcode: configured header file (.h.in) not found

I'm working through the CMake tutorial and I'm up to the Adding a Version Number and Configured Header File section. Unfortunately Xcode isn't recognising the generated header file:
The error:
The header file was generated by CMake ok:
TutorialConfig.h.in
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR #Tutorial_VERSION_MAJOR#
#define Tutorial_VERSION_MINOR #Tutorial_VERSION_MINOR#
CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
"${PROJECT_BINARY_DIR}/TutorialConfig.h"
)
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}/src")
# add the executable
add_executable(Tutorial src/tutorial.cpp)
This is the tutorial I'm working from.
I don't use Xcode, but another IDE that's Linux based. But maybe I can help you a bit. I'm just wondering -- does your program compile? That's perhaps more important.
My IDE often complains about header files such as your's -- that is, configuration files that are generated by CMake. Such warnings (at least for me) can be ignored. I usually do because my build/ directory is sometimes empty. So when I've opened up the source file, there wouldn't be TutorialConfig.h yet.
In fact, the IDE can never know about such header files. That's because it doesn't know which build/ directory you're going to compile your program in.
If you are worried by the warning, then there is probably a place where you can specify to Xcode which build/ directory to search for header files. That will remove the warning, after you've run CMake once. However, removing such a warning is unreliable because you can always build in another directory that would have TutorialConfig.h missing.
Hope this helps!
Oh, haha. I changed #include "src/TutorialConfig.h" to #include "TutorialConfig.h" and all is well. I figured it out by checking out the project settings:

The right way to structure my c++ project with cmake?

I have been struggling with this for quite a while, and my adventures with cmake have only resulted in hackish solutions that I am pretty sure are not correct.
I created a library that consists of several files, as follows:
-libfolder
-codepart1folder
-CMakeLists.txt
-codepart1.cpp
-codepart1.hpp
-codepart2folder
-codepart3folder
-lib.cpp
-lib.hpp
-CMakeLists.txt
I wrote a CMakeLists file to compile the library (after some experimentation), and I can generate a lib.a file. Now I would like to include this code as a library in other projects, and access it through the interface in lib.hpp. What is the best way to do this, in terms of directory structure, and what I need to put into CMakeLists.txt in my root project?
My current attempt has been to add -libfolder as a subfolder to my current project, and add the commands:
include_directories(${PROJECT_SOURCE_DIR}/libfolder)
link_directories(${PROJECT_BINARY_DIR}/libfolder)
add_subdirectory(libfolder)
target_link_libraries(project lib)
When I run make, the library compiles fine, but when project.cpp compiles, it complains that it cannot find codepart1.hpp (which is included in lib.hpp, included from project.cpp).
I suspect that this is the wrong way about doing this, but I cannot wade through the CMake documentation and find a good tutorial on setting up projects like this. Please help, CMake gurus!
The clean way to import one CMake project into another is via the find_package command. The package declaration is done by using the export command. An advantage of using find_package is that it eliminates the need to hard-code paths to the package's files.
Regarding the missing hpp file, you didn't include codepart1folder, so it's not on the include path.
Ok, so after consulting a coworker of mine who is a CMake guru, it seems CMake does not have support for what I am trying to do, leaving one with 3 options:
Add all of the dependencies to the parent projects CMakeLists.txt - not very clean, but it will get the thing to work. You'll have to do this for every project you add the code to, and go back and fix things if your library changes.
clean up your library headers. This is done through some compiler hackery. The idea is to forward-declare every class, and use only pointers or boost::shared_ptr, and then include the dependencies only in the cpp file. That way you can build the cpp file using all the findpackage stuff, and you get the bonus of being able to use the lib by only including the header and linking to the library.
Look into build systems. Having portable code and fast code compilation with complex dependencies is not a solved problem! From my investigations it turned out to be quite complicated. I ended up adopting my coworkers build system which he created himself in cmake, using things he picked up from Google.
Looking at your post you don't seem to add 'codepart1folder' to the includes anywhere. How are you including codepart1.hpp as:
#include <codepart1.hpp>
#include "codepart1folder/codepart1.hpp"
I don't think there is a standard accepted way to structure cmake projects. I've looked at a bunch of cmake repos and they tend to have differences. Personally I do the following:
-project
CMakeLists.txt
-build
-cmake
OptionalCmakeModule.cmake
-src
-Main
Main.cpp
Main.hpp
-DataStructs
SomeTree.hpp
SomeObject.hpp
-Debug
Debug.hpp
-UI
Window.hpp
Window.cpp
Basically that dumps all the source code into 1 directory, then you perform an out of source build with: 'mkdir build && cd build && cmake .. && make' in the projects root folder.
If you have separate libs as part of your project, then you might want a separate libs directory with another subfolder for your specific lib.
I have some of my repos on: https://github.com/dcbishop/ if you want to look at the CMakeLists.txt files.
The main problems with my project structure are that I use the FILE_GLOB which is apparently the 'wrong' way to do things (if you add files after running 'cmake ..' then they won't be picked up hen you do a 'make'). I haven't figured out what the 'right' way to do it is (from what I can see it involves keeping a separate list of files) I also only use 1 CMakeLists.txt file.
Some projects also choose to separate their cpp and hpp files into separate directories. So you would have an include and src folders (at least for the hpp files that are intended to be used externally). I think that would mainly be for projects that are mainly large libraries. Would also make installing header files much easier.
You are probably missing
include_directories(${PROJECT_SOURCE_DIR}/libfolder/codepart1folder)
In such a case you might want to set( CMAKE_INCLUDE_CURRENT_DIR on) to add all folders to the include directory path variable.
Check cmake's output on the command line whether the correct include folders are set or not. Additionally you can always use message() as "print debugging" for cmake variables.
In case of include directories however you need to read the directory property to see what is actually in the include directories.
get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
message("inc_dirs = ${inc_dirs}")
I hope this helps you figuring out what is missing.
Edit
I just saw your comment about added codepart1folder in the libfolder. It is only available in the libfolder's include_directory path and not propagated to the root folder.
Since the include codepart1.hpp is present in the lib.hpp however you need to have it also available in the project path otherwise you will get missing declaration errors when you build your project.

Autotools: Switching part of project to a different compiler

I'm working on a big, old, project that is using autotools. I would like to switch one part of the project to C++ (from C).
How can I switch the compiler used for a part of the project? I don't like the idea of completely splitting the project into two parts. The directory has only Makefile.am and I suppose I should register this somehow in configure.ac.
You must define output variable CXX in configure.ac (the simplest way is by using AC_PROG_CXX macro), then all files with appropriate suffixes (.cc, .cpp) will be compiled by C++ compiler.
In your source root, you mut have a configure.ac and Makefile.am with all the compiler options. So, you can create a subdirectory with a new configure.ac and Makefile.am with other options.
You only need to add this line in the parent configure.ac
AC_CONFIG_SUBDIRS([subdir_name])
And add the new subdirectory in the parent Makefile.am
SUBDIRS= subdir_name
Hope can help.