Make CMake use gccfilter - gcc

GCCFilter is a neat perl script that allows to color the output of GCC and thus makes debugging much more fun and, more important, faster.
You can use GCCFilter with a CMake generated Makefile by calling
gccfilter -a -c make
However, this approach has some drawbacks: Delayed output of CMake status infos, no color in the CMake commands are the most obvious.
The question: Is there a way to write some CMake module that searches for gccfilter if the compiler is gcc, checks if, say COLOR_CXX is set (rather easy up to here) and then tells CMake to replace all calls to gcc by gccfilter -a -c gcc.
CMake offers the variable CMAKE_CXX_COMPILER, but changing this one will disallow CMake to find correct include paths and the like. Is there some variable we may change after the project() command that is prefixed before each call to gcc?

You can make CMake use gccfilter by pointing the RULE_LAUNCH_COMPILE property to a wrapper script which invokes gccfilter with the desired options.
Create an executable shell script named gccfilter_wrap in the outermost CMake project directory with the following contents:
#!/bin/sh
exec gccfilter -a -c "$#"
Be sure to set the file's executable bit. Then in your CMakeLists.txt, set the RULE_LAUNCH_COMPILE directory property before adding targets:
project (HelloWorld)
set_directory_properties(PROPERTIES RULE_LAUNCH_COMPILE
"${PROJECT_SOURCE_DIR}/gccfilter_wrap")
add_executable(HelloWorld HelloWorld.cpp)
The generated makefile rules will then prefix each compiler invocation with the gccfilter_wrap script. Alternatively the RULE_LAUNCH_COMPILE property can also be set as a target property or as global property.
The RULE_LAUNCH_COMPILE property only works for Makefile-based CMake generators.
Edit by Thilo
This is how I finally solved the problem - basically a rephrased version of this solution:
# GCCFilter, if appliciable
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCPP)
option(COLOR_GCC "Use GCCFilter to color compiler output messages" ON)
set(COLOR_GCC_OPTIONS "-c -r -w" CACHE STRING "Arguments that are passed to gccfilter when output coloring is switchend on. Defaults to -c -r -w.")
if(COLOR_GCC)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${PROJECT_SOURCE_DIR}/cmake/gccfilter ${COLOR_GCC_OPTIONS}")
endif()
endif()

Related

Inhibit all warnings from `make`

I am building Android AOSP on my server running Ubunty 16.04. I have all dependencies installed and the build completes fine. However, I get many Warnings from the make command each time. Is there a way to inhibit those from the console output (eg. via a parameter passed into the command). I have tried with some gcc flags in an attempt but the compiler either ignored them or threw errors due to unrecognised parameters.
Thank you for the kind help.
Lorenzo
gcc has the -w option which suppresses all warnings. The warning has to be put after any other compiler flags. How you do that depend on the makefile recipes. A late CXXFLAGS += -w might work. Put the -w behind a by default empty variable and you can override from the make invocation.
Makefile:
...
EXTRA_FLAGS ?=
# CXXFLAGS is a convention, your flag variables may be named differently
CXXFLAGS += $(EXTRA_FLAGS)
Make invocation
make my_target EXTRA_FLAGS="-w"
You can also set the environment variable EXTRA_FLAGS to set implicitly for each invocation:
export EXTRA_FLAGS="-w"
make my_target
export EXTRA_FLAGS=""
make my_target
Caveats include conflict with -Werror but guessing you are not using that since it compiles with warnings.

How to get CMake variable of package in shellscript

I want to find the Qt5WaylandClient_PRIVATE_INCLUDE_DIRS variable which is set by the Qt5WaylandClient package. How can I get it from the shell (dash)? Something like this:
cmake -find_package(Qt5WaylandClient) -get_variable Qt5WaylandClient_PRIVATE_INCLUDE_DIRS
or
cmake -file path/to/my/CMakeLists.txt -get_variable Qt5WaylandClient_PRIVATE_INCLUDE_DIRS
CMake does have a --find-package command line option, but it is not well supported, nor well-documented. There is an answer describing its functionality here, but that's probably not what you're looking for.
Initially, it appears you could just run cmake in script mode, using -P, on a CMake file containing your find_package(Qt5WaylandCleint) command and print its variables to the console.
cmake -P MyFindQt5WaylandClient.cmake
However, running find_package() outside the confines of a CMake project does not work. It yields several errors because CMake doesn't know anything about the system or your target language. So, you must create a minimal CMake project, then run find_package(). Something like this CMakeLists.txt file should work:
cmake_minimum_required(VERSION 3.16)
project(MyProj)
find_package(Qt5WaylandClient REQUIRED)
# Print the desired variable.
message("${Qt5WaylandClient_PRIVATE_INCLUDE_DIRS}")
You can then run cmake from the command line, and this will print the Qt5WaylandClient_PRIVATE_INCLUDE_DIRS variable to the console. You can use the -S and -B command line options to specify the CMake source and binary directories, respectively.
cmake -S . -B build

Configure GCC to add compile flags globally

Can I configure GCC to add some file globally, for every project? I want to make it temporarily and only with flags like: -fdiagnostics-color.
I don't understand why do you need it but you can do a wrapper:
which gcc - will print a patch to GCC (copy it to clipboard)
mkdir somedir; cd somedir
create file with name gcc
and add into it: full path to gcc(from clipboard) -fdiagnostics-color somefile.c $# this command will add -fdiagnostics-color somefile.c before every line that came to gcc.
chmod +x gcc - set execution rights to gcc wrapper
And finally
add path to your wrapper. export PATH=somedir:$PATH
You might read about GCC spec files and alter the spec file used by your particular version of gcc. But this is generally frowned upon.
The usual practice would be to use GNU make and add a CFLAGS += -fdiagnostics-color to your Makefile. BTW with a recent enough GCC this (adding -fdiagnostics-color flag) is not even necessary since (at least by setting your GCC_COLORS environment variable) the default is -fdiagnostics-color=auto

Set gcc and g++ optimization flags permanently

I'm running a x86 kernel on an x64 machine. I would like to compile libraries for a i586 processor. During compilation, some libraries use i686 optimization, so want to set -mtunes=i586, -march=i586 and -O3 flags for all of libraries even if they explicitly declare something else in their makefiles.
Somehow I want to set compiler flags permanently...
Regardless of whether you should do this, here's the easiest way to do it:
Create a new file with the following contents:
#!/bin/sh
exec /usr/bin/gcc "$#" -O3 -mtunes=i586 -march=i586
Change /usr/bin/gcc to your actual compiler if that's not right on your system.
Save it as ~/bin/gcc.
Make the new script executable:
chmod +x ~/bin/gcc
Repeat to create another file for g++.
Add ~/bin to the start of your path:
export PATH=~/bin:$PATH
Compile your project. Whenever your new scripts are on the path they will override whatever the makefile says.
Hope that helps.
P.S. The best way to do it (rather than the easiest) would probably be to mess with the compiler's "specs" file, but it's much harder to explain and do.

Is there a way to make a c++ compiler flag as default

Just like we specify input flags in the settings of the project in Xcode
Can I make few flags like -O3 or -fopenmp as default flags in command line when I use Terminal.
So that I dont have to type them everytime I compile some c++ fies. Is there a file in the installed gcc or C++ that I can edit to make them default.
Please let me know
thanks
For situations like this you'd probably use a makefile if it's project specific (or other similar automated build management like scons or cmake).
If you want it always on the terminal, you can alias your command to always specify those options, i.e.
alias g++='g++ -O3 -fopenmp'
Note that you said 'terminal' so I assume this is a type of *nix. If that is the case you can also set this into your terminal profile, like ~/.bashrc if you use bash, or ~/.zshrc if you use zsh, etc.

Resources