I have directory aaa and aaa/bb. I want CMake to create the moc_compilation.o file only for aaa/*.cxx files, but it scans recursively aaa/bb/*.cxx files also. How can I disable the recursive scan for automoc?
You can use the SKIP_AUTOMOC source file property to skip automoc processing for one file, or an entire group of files. This works for header files as well:
file(GLOB MY_EXCLUDED_SOURCES aaa/bb/*.cxx)
set_property(SOURCE ${MY_EXCLUDED_SOURCES} PROPERTY SKIP_AUTOMOC ON)
Related
I have a project that uses some third party libraries. So each time I setup this project with CMake, I have to set each entry (path of the third party library) on the GUI of CMake. I improve this by making CMake script guess the path by this script (learn this technique from OGRE):
# Guess the paths.
set( OGRE_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies/Ogre" CACHE STRING "Path to OGRE source code (see http://www.ogre3d.org/tikiwiki/tiki-index.php?page=CMake+Quick+Start+Guide)" )
So each time I setup with CMake, it will automatic fill the entry OGRE_SOURCE. But that doesn't enough. If the Ogre source is not in the path
"${CMAKE_CURRENT_SOURCE_DIR}/Dependencies/Ogre"
, then I have to open and edit the CMake script or I have to edit the entry on the GUI of CMake. I find that pretty inconvenient, especially when you link to a lot of third party libraries.
So I want to use another technique: preset settings for entries from file - CMake reads the presets from file PresetEntries.txt (that I make) and apply the these presets on the entries (It's a lot quicker to edit the path in text file than on the GUI of CMake).
Here my idea about this preset file: PresetEntries.txt
OGRE_SOURCE=E:/Source/ogre
I found that CMake can read a text file, but if I use this, I have to do string manipulations.
CMake has the file CMakeCache.txt to save the settings on the CMake GUI, but I want it to be simple: it should only has the preset settings that need to be pre-set.
So I wonder if CMake support this preset settings for entries from file.
Edit:
So I read this question and see that CMake can set config from file, but this require to fire cmake with the -C mysettings.cmake, but I wanna it to be automatically with CMake GUI - just edit the file and hit generate button in CMake GUI. So I wanna make this question more specific:
In my CMakeLists.txt should have script like this:
# Guess the paths.
#I wanna have this function from C++
#https://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
GetPrivateProfileString("OGRE", #lpAppName
"OGRE_SOURCE", #lpKeyName
"${CMAKE_CURRENT_SOURCE_DIR}/Dependencies/Ogre", #lpDefault
OGRE_SOURCE_VAR,#lpReturnedString
MAX_PATH, #nSize, may be can reduce this variable
"LibPath.ini") #lpFileName
set( OGRE_SOURCE "${OGRE_SOURCE_VAR}" CACHE STRING "Path to OGRE source code" )
In the file LibPath.ini
[OGRE]
OGRE_SOURCE = "E:/Source/ogre"
So the user can choose to either use the ini file or not.
I don't know if there any way I can use a function that similar to function GetPrivateProfileString (of C++) in CMake.
Thanks for reading
The external libraries should be included by one of the following commands
find_package(ttnlib REQUIRED HINTS /usr/local/lib/cmake)
include_directories(${ttnlib_INCLUDE_DIR})
set(EXTRA_LIBS ${EXTRA_LIBS} ${TTNLIB_LIBRARY})
or
find_library(MY_EXTERNAL_LIB name COOLSTUFF libCOOLSTUFF libCOOLSTUF.so hints /usr/local/lib)
The search for the external packages and libraries should only be necessary for the first run of cmake.
I can't find the function to read the ini file, so what I can do is create a simple function that read simple txt file for myself.
I declare the function in 1 file for other file use it
"\CMake\Dependencies\CommonFunc.cmake"
#------------Define function Read file------------
macro( readSettingFile KEY DEFAULT_RESULT STRING_RESULT_OUT)
unset(STRING_RESULT)
# Read the file
file( READ "${CMAKE_SOURCE_DIR}/LibPath.txt" LIB_PATH_STR )
# Set the variable "Esc" to the ASCII value 27 - basically something
# which is unlikely to conflict with anything in the file contents.
string(ASCII 27 Esc)
# Turn the contents into a list of strings, each ending with an Esc.
# This allows us to preserve blank lines in the file since CMake
# automatically prunes empty list items during a foreach loop.
string(REGEX REPLACE "\n" "${Esc};" LIB_PATH_LINES "${LIB_PATH_STR}")
foreach(LINE ${LIB_PATH_LINES})
if("${LINE}" MATCHES "${KEY}")
#remove the key, leave the content untouch
string(REPLACE "${KEY}" "" STRING_RESULT ${LINE})
# Swap the appended Esc character back out in favour of a line feed
string(REGEX REPLACE "${Esc}" "" STRING_RESULT ${STRING_RESULT})
endif()
endforeach()
if("${STRING_RESULT}" STREQUAL "")
set( STRING_RESULT ${DEFAULT_RESULT} )
endif()
#message( STATIC "---GTA Sa-----" "[${STRING_RESULT}]" )
endmacro()
(I need the help from this answer to write this function :p)
Here is how I use
For example: "\CMake\Dependencies\Ogre.cmake"
#include common functions
include( CMake/Dependencies/CommonFunc.cmake )
#---------------Guess the paths.----------------------
#----Set OGRE_SOURCE
readSettingFile( "OGRE_SOURCE="
"E:/Source/ogre"
STRING_RESULT
)
set( OGRE_SOURCE "${STRING_RESULT}" CACHE STRING "Path to OGRE Source" )
#----Set OGRE_BINARIES
readSettingFile( "OGRE_BINARIES="
"E:/Source/_build/ogre"
STRING_RESULT
)
set( OGRE_BINARIES "${STRING_RESULT}" CACHE STRING "Path to OGRE's build folder generated by CMake" )
Here is the setting file
"\LibPath.txt"
OGRE_SOURCE=E:/Source/ogre
OGRE_BINARIES=E:/Source/_build/ogre
I have a directory tree like this:
libs
support
db
csv
patterns
support_qt
helpers
dialogs
etc.
Now when I do the add_subdirectory in the support level, I can add db and patterns and the files are collected. However in db I added another add_subdirectory referencing the csv, but somehow this is ignored.
In support
set(SUPPORT_SOURCE
${CMAKE_CURRENT_SOURCE_DIR}/support_defs.h
${CMAKE_CURRENT_SOURCE_DIR}/support_dll_api.h
${CMAKE_CURRENT_SOURCE_DIR}/supportlib_namespace.h
${CMAKE_CURRENT_SOURCE_DIR}/dll_main.cpp
)
add_subdirectory (db)
add_subdirectory (patterns)
In db
set(SUPPORT_SOURCE ${SUPPORT_SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/column_types.h
${CMAKE_CURRENT_SOURCE_DIR}/dbcolumn.h
${CMAKE_CURRENT_SOURCE_DIR}/database_login.h
${CMAKE_CURRENT_SOURCE_DIR}/database_login.cpp
${CMAKE_CURRENT_SOURCE_DIR}/type_helper.h
${CMAKE_CURRENT_SOURCE_DIR}/type_helper.cpp
PARENT_SCOPE
)
add_subdirectory(csv)
The above works fine but in csv
set(SUPPORT_SOURCE ${SUPPORT_SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/csv.h
${CMAKE_CURRENT_SOURCE_DIR}/csv.cpp
PARENT_SCOPE
)
But these files are not included in the build. So do I have to put the add_subdirectory calls all into the root file?
Just found the solution. I have to put the add_subdirectory before the set command.
add_subdirectory(csv)
set(SUPPORT_SOURCE ${SUPPORT_SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/column_types.h
${CMAKE_CURRENT_SOURCE_DIR}/dbcolumn.h
${CMAKE_CURRENT_SOURCE_DIR}/database_login.h
${CMAKE_CURRENT_SOURCE_DIR}/database_login.cpp
${CMAKE_CURRENT_SOURCE_DIR}/type_helper.h
${CMAKE_CURRENT_SOURCE_DIR}/type_helper.cpp
PARENT_SCOPE
)
I have a directory with files that either belong to a set that makes up a Qt project, and other files that do not. That is, files A.cxx, ADriver.cxx and A.ui all belong to a set that needs to be compiled with Qt options. I then have a file B.cxx that is non-qt. Then C.cxx, CDriver, and C.ui are another Qt set. There are tens of these, so I want to use globs rather than write each add_executable manually. I was thinking of doing something like
for(all ui files)
create an executable from the ui and its matching .cxx and *Driver.cxx"
end
Then all cxx files that "remain" (not used in the above loop) are non-Qt, and need to be compiled by themselves. My question is how to "subtract" files from a "set". That is, to use the method described above I'd have to have a set of all cxx files, and remove the ones that get used in the .ui file loop. Is this possible? Is there a better way to do something like this?
First, gather all files with a glob:
file(GLOB ALL_SRCS *)
Then select ui files and create Qt targets for them, substracting them from the ALL_SRCS list at the same time:
file(GLOB UIS *.ui)
foreach(ui ${UIS})
get_filename_component(f ${ui} NAME_WE)
# Do Qt stuff
qt4_wrap_ui( ${f}uis ${ui} )
qt4_wrap_cpp( ${f}srcs ${f}.cpp ${f}Driver.cpp )
add_executable( ${f} ${f}uis ${f}srcs )
list(REMOVE_ITEM ALL_SRCS ${ui} ${f}.cpp ${f}Driver.cpp)
endforeach()
After this you'll have all non-qt sources in ALL_SRCS.
I want to include all filed in source directory leaving one file.
Is there any way to using aux_source_directory or anything else I can include all files leaving that file ?
There are two possible solutions:
Use file (GLOB ... instead of aux_source_directory with a globbing expression that does not match that one file but includes all the others, e.g.:
file(GLOB _srcFiles "src/f[1-3].cpp")
This will match match files f1.cpp, f2.cpp, f3.cpp, but not f4.cpp.
Or use aux_source_directory and then remove the file to be excluded explicitly with a list(REMOVE_ITEM command, e.g.:
aux_source_directory(src _srcFiles)
list(REMOVE_ITEM _srcFiles "src/f4.cpp")
I was wondering if there was a way (such as a commad) to move a directory filled with, say, image files, to the build directory using cmake 2.8.
Thanks in advance!
The file() command can do what you want.
From the cmake manual:
The file() command also provides COPY and INSTALL signatures:
file(<COPY|INSTALL> files... DESTINATION <dir>
[FILE_PERMISSIONS permissions...]
[DIRECTORY_PERMISSIONS permissions...]
[NO_SOURCE_PERMISSIONS] [USE_SOURCE_PERMISSIONS]
[FILES_MATCHING]
[[PATTERN <pattern> | REGEX <regex>]
[EXCLUDE] [PERMISSIONS permissions...]] [...])
The COPY signature copies files, directories, and symlinks to a destination fold Relative input paths are evaluated with respect to the current source directory, and a relative destination is evaluated with respect to the current build directory. Copying preserves input file timestamps, and optimizes out a file if it exists at the destination with the same timestamp. Copying preserves input permissions unless explicit permissions or NO_SOURCE_PERMISSIONS are given (default is USE_SOURCE_PERMISSIONS). See the install(DIRECTORY) command for documentation of permissions, PATTERN, REGEX, and EXCLUDE options.
So you would have something like (tested):
file(COPY ${YOUR_SRC_IMAGE_DIR} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/YourPreferedDestination)
To move, you can use the RENAME form:
file(RENAME ${YOUR_SRC_IMAGE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/PreferedDestination)
But I am not sure that you would want that, because the source will not be available anymore to reproduce the build sequence, hence my attempt to answer with the copy command above.