cmake option default value if else - cmake-gui

I tried to do that, but it doesn't appear on the cmake gui, how have I got to do to change the default value of option depending of S.O, and it appear in the cmake gui?
if(UNIX)
option(HASH "enable hash" ON)
else()
option(HASH "enable hash" OFF)
endif()

Unless you unset or mark_as_advanced the HASH variable, it should appear in the CMake GUI.

Related

Why does CMake set -no-fat-lto-objects when I enable LTO/IPO?

I'm enabling IPO (inter-procedural optimization) for a C compilation of mine, using CMake:
set_property(TARGET foo PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
As expected, this causes an -flto compiler flag to be added. However, it also adds -fno-fat-lto-objects: That means that the resulting object file will only have intermediate code, rather than both properly-compiled and intermediate code; and that means that the linker must support my system compiler's intermediate representation and be IPO/LTO-aware.
I didn't ask for -fno-fat-lto-objects, nor did I want it. Can I get CMake to not add this option?
IMNSHO opinion this is a CMake bug... which I have filed as:
https://gitlab.kitware.com/cmake/cmake/-/issues/23136
The developers have simply made the incorrect assumption that this is what people want.
if(CMAKE_C_COMPILER MATCHES "GNU")
set(CMAKE_C_COMPILE_OPTIONS_IPO "-flto")
endif()
How to find it:
Navigate to your CMake installation directory and to Modules, most of the stuff is there.
It's /usr/share/cmake/Modules on my Linux system
Find the string or similar string that you are interested in
on my system, I do:
$ grep fno-fat-lto-objects -r .
./Compiler/GNU.cmake: list(APPEND __lto_flags -fno-fat-lto-objects)
Navigate and inspect the resulting files, the context where the string is used:
# '-flto' introduced since GCC 4.5:
# * https://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Option-Summary.html (no)
# * https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Option-Summary.html (yes)
if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.5)
set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
set(__lto_flags -flto)
if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.7)
# '-ffat-lto-objects' introduced since GCC 4.7:
# * https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Option-Summary.html (no)
# * https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gcc/Option-Summary.html (yes)
list(APPEND __lto_flags -fno-fat-lto-objects)
endif()
set(CMAKE_${lang}_COMPILE_OPTIONS_IPO ${__lto_flags})
Come up with a workaround to implement custom behavior of such coe.

CMake does not generate .ts files

I am trying to generate *.ts files with my CMake, but it does nothing. It even do not show any message. When I am trying directly with lupdate, it is working, so I don't know where the problem might be.
Here my CMakeLists.txt:
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
project(Band VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(Qt5_DIR "C:/QtOpen/5.13.2/msvc2017_64/lib/cmake/Qt5")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS Designer)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set ( SOURCES
Data/Band.cpp
MainWindow.cpp
)
set ( MOC_HEADERS
Data/Band.h
MainWindow.h
)
set ( UIS Designer/band.ui)
set ( RESOURCES application.qrc )
find_package(Qt5 COMPONENTS Core Gui Widgets Xml Network PrintSupport LinguistTools REQUIRED)
qt5_create_translation(QM_FILES MainWindow.cpp english.ts)
add_executable( ${CMAKE_PROJECT_NAME} WIN32 ${SOURCES} ${MOC_HEADERS} ${RESOURCES} ${UIS} ${QM_FILES} icon.rc)
target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC DEBUG_MODE)
target_link_libraries(${CMAKE_PROJECT_NAME} Qt5::Widgets Qt5::Gui Qt5::Xml Qt5::PrintSupport)
Can anyone help? I went through all the tutorials, documentation and answers about this topic, but found nothing that could explain why it does not generate .ts file for me. Thanks in advance.
ref: https://doc.qt.io/qt-5/qtlinguist-cmake-qt5-create-translation.html
I think the problem is that just listing ${QM_FILES} as one of the sources in add_executable doesn't force CMake to understand that it needs to generate those files. I guess this is because normally source files are not generated.
You can add the dependency more explicitly via a custom target. After the qt5_create_translation line, add this:
add_custom_target(translations DEPENDS ${QM_FILES})
Then, after the add_executable line, add this:
add_dependencies(${CMAKE_PROJECT_NAME} translations)
Essentially this says there is a target that depends on the generated translation files and that your executable depends on this target, and thus on the generation of the translation files. This should be enough to force qt5_create_translation to be invoked.

Setting clang default options?

My copy of clang seems to run with options like -Wimplicit-int and -Wreturn-type turned on by default. (Perhaps this is because the language is set to c11 by default.) I know I can turn these
off using -Wno-implicit-int and -Wno-return-type, but is there a way to do that by default? Maybe with an environment variable or rc file?
I'd also like to add a few options, like having it search /usr/local/include by default.
I'd also like turn off the -fcolor-diagnostics, -fshow-column, and -fno-caret-diagnostics options, which also seem to be on by default.
(This is clang-600.0.57 / Apple LLVM version 6.0 under MacOS
10.9.5, if it matters.)
As describe here you can use the MAKEFILES environment to define extra default target and flags (like a makefile include ), to define the default CFLAGS then use += to add extra param in your project specific CFLAGS

How to generate assembly listings in Code::Blocks?

Can we generate an assembly file in code-blocks for C programs, the same way we can generate listing file when we write C-code for micro-controller?
I presume you want your Code::Blocks build to generate an annotated
assembly listing for each .c source file compiled.
Yes, you can do this as follows (as of Code::Blocks 12.11):
In the Code::Blocks IDE, navigate Settings -> Compiler
Ensure that the Selected compiler is GCC.
In the tab list that begins Compiler settings, Linker settings...,
navigate to the last tab, Other settings.
In the Other settings pane, select Advanced options, at the bottom right.
In the Advanced compiler options windows, ensure that the drop-down menu
entitled Command is set at Compile single file to object file
See the edit box entitled Command line macro and confirm that it
contains the line:
$compiler $options $includes -c $file -o $object
To this line append a space followed by exactly this:
-Wa,-alhds=$objects_output_dir$file_name.list
(Do not allow yourself to be misled at this point by the listing of
Command macros at the left of the window. If it lists $objectsoutputdir
instead of $objects_output_dir and $filename instead of $file_name, it
is wrong.)
Click OK in the Advanced compiler options window and then in the
Compiler settings window.
Rebuild your project.
The effect of the change you have made to the global compiler settings
is that the options:
-alhds=$objects_output_dir$file_name.list
will be passed to the assembly phase when compiling each source file filename.c,
with $objects_output_dirreplaced by the object output files directory of your project
and $file_name replaced by filename, causing the assembler to generate an
annotated listing file filename.list in the same directory where filename.o is placed.
Unfortunately I do not see how to do this just for a selected project; so after
the change the compiler will generate the .list files for all projects.
If this is a nuisance, you can work around it by first configuring a "new compiler"
in Code::Blocks that is a copy of GCC, with whatever name you like, and then applying
the change I have described to this copy. To do this:
Navigate Settings -> Compiler;
Ensure the Selected compiler is GCC
Select Copy and enter the name of your choice when prompted, e.g.
"gcc-list"

How to correctly link libgit2 to C program using cmake?

First of all, i'm just a newbie in a CMake magic. And i just want to link libgit2 to my simple C program in CMake way (FindLibgit2.cmake).
As i understand from cmake documentation my CMakeLists.txt should looks like that:
project(libgit2test)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
# This variables should be setting up externally, i know - i show them just for example
set (LIBGIT2_LIBRARIES "<path to directory with git2.lib and git2.dll")
set (LIBGIT2_INCLUDE_DIR "<path to libgit2/include>")
find_package(Libgit2 REQUIRED)
include_directories(${LIBGIT2_INCLUDE_DIR})
set(LIBS ${LIBS} ${LIBGIT2_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${LIBS})
In my simple program i just call a couple of simple libgit2 functions and get this:
WARNING: Target "libgit2test" requests linking to directory "<libgit2 build directory>". Targets may link only to libraries. CMake is dropping the item.
I think the problem is here: target_link_libraries(${PROJECT_NAME} {$LIBS})
I try to change it to git2, but than i just get can not open file.
What i'm doing wrong?
P.S. I'm using Visual Studio 2010 compiler, Qt Creator to create CMake project, and successfully build libgit2 with CMake.
Remove
set (LIBGIT2_LIBRARIES "<path to directory with git2.lib and git2.dll")
set (LIBGIT2_INCLUDE_DIR "<path to libgit2/include>")
These variables should be set by find_package(Libgit2 REQUIRED).
I've found the source of problem: variable LIBGIT2_LIBRARIES must point to lib file itself, not the directory of it (debug/release).
However, setting up manually this variables looks like wired. I want to find more "automatic" way to find libgit2 - if one exists.

Resources