1
Getting started with CMake
2
3
[Windows instructions at the end of this document]
4
5
(These instructions apply to Unix-like systems, including Cygwin and Mac. To
6
build using Visual Studio or some other IDE supported by CMake, most of the
7
information below still applies)
8
9
Always compile in a separate directory to the code. For example, if the
10
code (eg, from Git) is at /home/curt/projects/flightgear, you might create
11
/home/curt/projects/fgbuild. Change into the new directory, and run
12
13
    cmake ../flightgear
14
    
15
To generate standard Unix Makefiles in fgbuild.
16
17
Probably you want to specify an install prefix:
18
19
    cmake ../flightgear -DCMAKE_INSTALL_PREFIX=/usr
20
21
Note the install prefix is automatically searched for required libraries
22
and header files, so if you install PLIB, OpenSceneGraph and SimGear to the
23
same prefix, most configuration options are unnecessary.
24
25
If for some reason you have a dependency (or several) at a different prefix,
26
you can specify one or more via CMAKE_PREFIX_PATH:
27
28
    cmake ../flightgear -DCMAKE_PREFIX_PATH="/opt/local;/opt/fgfs"
29
30
(note the use of semi-colons to specify multiple prefix paths)
31
32
Standard prefixes are searched automatically (/usr, /usr/local, /opt/local)
33
34
Most dependencies also expose an environment variable to specify their
35
installation directory explicitly eg OSG_DIR or PLIBDIR. Any of the methods
36
described above will work, but specifying an INSTALL_PREFIX or PREFIX_PATH is
37
usually simpler.
38
39
By default, we select a release build. To create a debug build, use
40
41
    cmake ../flightgear -DCMAKE_BUILD_TYPE=Debug
42
    
43
(or MinSizeRel, or RelWithDbg)
44
45
Debug builds will automatically use corresponding debug builds of required
46
libraries, if they are available. For example you can install debug builds of
47
SimGear and OpenSceneGraph, and a debug FlightGear build will use them.
48
49
(Debug builds of libraries have the 'd' suffix by default - Release builds
50
have no additional suffix)
51
52
Note most IDE projects (eg Xcode and Visual Studio) support building all the
53
build types from the same project, so you can omit the CMAKE_BUILD_TYPE option
54
when running cmake, and simply pick the build configuration as normal in the
55
IDE.
56
57
It's common to have several build directories with different build
58
configurations, eg
59
60
    /home/curt/projects/flightgear (the git clone)
61
    /home/curt/projects/fgdebug
62
    /home/curt/projects/fgrelease
63
    /home/curt/projects/fg-with-svn-osg
64
65
To set an optional feature, do
66
67
    cmake ../flightgear -DFEATURE_NAME=ON 
68
69
(or 'OFF' to disable )
70
71
To see the variables that can be configured / are currently defined, you can
72
run one of the GUI front ends, or the following command:
73
74
    cmake ../flighgear -L
75
76
Add 'A' to see all the options (including advanced options), or 'H' to see
77
the help for each option (similar to running configure --help under autoconf):
78
79
    cmake ../flightgear -LH
80
81
Build Targets
82
83
For a Unix makefile build, 'make dist', 'make uninstall' and 'make test' are
84
all available and should work as expected. 'make clean' is also as normal,
85
but there is *no* 'make distclean' target. The equivalent is to completely
86
remove your build directory, and start with a fresh one.
87
88
Adding new files to the build
89
90
Add source files to the SOURCES list, and headers to the HEADERS list. Note
91
technically you only need to add source files, but omitting headers confuses
92
project generation and distribution / packaging targets.
93
94
For target conditional files, you can append to the SOURCES or HEADERS lists
95
inside an if() test, for example:
96
97
    if(APPLE)
98
    	list(APPEND SOURCES extraFile1.cxx extraFile2.cxx)
99
    endif()
100
101
Setting include directories
102
103
In any CMakeList.txt, you can do the following:
104
105
    include_directories(${PROJECT_SOURCE_DIR}/some/path)
106
107
For example, this can be done in particular subdirectory, or at the project
108
root, or an intermediate level.
109
110
Setting target specific compile flags, includes or defines
111
112
Use set_target_property(), for example
113
114
    set_target_property(fgfs PROPERTIES
115
            COMPILE_DEFINITIONS FOO BAR=1)
116
            
117
You can set a property on an individual source file:
118
119
    set_property(SOURCE myfile.cxx PROPERTY COMPILE_FLAGS "-Wno-unsigned-compare")
120
            
121
Detecting Features / Libraries
122
123
For most standard libraries (Gtk, wxWidget, Python, GDAL, Qt, libXml, Boost),
124
cmake provides a standard helper. To see the available modules, run:
125
126
     cmake --help-module-list
127
128
In the root CMakeLists file, use a statement like:
129
130
    find_package(OpenGL REQUIRED)
131
    
132
Each package helper sets various variables such aaa_FOUND, aaa_INCLUDE_DIR,
133
and aaa_LIBRARY. Depending on the complexity of the package, these variables
134
might have different names (eg, OPENSCENEGRAPH_LIBRARIES).
135
136
If there's no standard helper for a library you need, find a similar one, copy
137
it to CMakeModules/FindABC.cmake, and modify the code to fit. Generally this
138
is pretty straightforward. The built-in modules reside in the Cmake 'share'
139
directory, eg /usr/share/cmake/modules on Unix systems.
140
141
Note libraries support by pkg-config can be handled directly, with no need
142
to create a custom FindABC helper.
143
            
144
Adding a new executable target
145
146
    add_executable(myexecutable ${SOURCES} ${HEADERS})
147
    target_link_libraries(myexecutable .... libraries ... )
148
    install(TARGETS myexecutable RUNTIME DESTINATION bin)
149
    
150
(If the executable should not be installed, omit the final line above)
151
152
If you add an additional line
153
154
    add_test(testname ${EXECUTABLE_OUTPUT_PATH}/myexecutable)
155
    
156
Then running 'make test' will run your executable as a unit test. The
157
executable should return either a success or failure result code.
158
159
160
161
SPECIAL INSTRUCTIONS TO BUILD UNDER WINDOWS WITH VISUAL STUDIO
162
==============================================================
163
164
On Windows, assumptions on the directory structure are made to automate the discovery of dependencies.
165
This recommended directory structure is described below:
166
167
${MSVC_3RDPARTY_ROOT} /
168
      3rdParty /                 ( includes plib, fltk, zlib, libpng, libjpeg, libtiff, freetype, libsvn, gdal, ...
169
         bin /
170
         include /
171
         lib /
172
      3rdParty.x64 /             ( 64 bit version )
173
         ...
174
      boost_1_44_0 /
175
         boost /
176
      install /
177
         msvc100 /               ( for VS2010 32 bits, or msvc90, msvc90-64 or msvc100-64 for VS2008 32, VS2008 64 and VS2010 64 )
178
            OpenSceneGraph /     ( OSG CMake install
179
               bin /
180
               include /
181
               lib /
182
            SimGear /
183
               include /
184
               lib /
185
186
If you do not use the recommended structure you will need to enter paths by hand. Source and build directories can be located anywhere.
187
188
The suggested inputs to cmake are :
189
      MSVC_3RDPARTY_ROOT : location of the above directory structure
190
      CMAKE_INSTALL_PREFIX : ${MSVC_3RDPARTY_ROOT}/install/msvc100/FlightGear (or any variation for the compiler version described above )
191
192
193
1. Set up a work directory as described above.
194
195
2. Open the Cmake gui.
196
197
3. Set "Where is the source code" to wherever you put the FlightGear sources (from the released tarball or the git repository).
198
199
4. Set "Where to build the binaries" to an empty directory.
200
201
5. Press the "Configure" button. The first time that the project is configured, Cmake will bring up a window asking which compiler you wish to use. Normally just accept Cmakes suggestion, and press Finish. Cmake will now do a check on your system and will produce a preliminary build configuration.
202
203
6. Cmake adds new configuration variables in red. Some have a value ending with -NOTFOUND. These variables should receive your attention. Some errors will prevent FlightGear to build and others will simply invalidate some options without provoking build errors. First check the MSVC_3RDPARTY_ROOT variable. If it is not set, chances are that there will be a lot of -NOTFOUND errors. Instead of trying to fix every error individually, set that variable and press the "Configure" button again.
204
205
7. Also check the lines with a checkbox. These are build options and may impact the feature set of the built program.
206
207
8. Change the CMAKE_INSTALL_PREFIX to ${MSVC_3RDPARTY_ROOT}/install/msvc100/FlightGear because C:\Program Files is likely unwritable to ordinary Windows users and will integrate better with the above directory structure (this is mandatory for SimGear if you don't want to solve errors by hand).
208
209
10. Repeat the process until the "Generate" button is enabled.
210
211
11. Press the "Generate" button.
212
213
12. Start Visual Studio 2010 and load the FlightGear solution (FlightGear.sln) located in "Where to build the binaries" (point 4.)
214
215
13. Choose the "Release" build in the VS2010 "Generation" toolbar
216
217
14. Generate the solution.
218
219
15. If there are build errors, return to Cmake, clear remaining errors, "Configure" and "Generate"
220
221
16. When Visual Studio is able to build everything without errors, build the INSTALL project to put the product files in ${CMAKE_INSTALL_PREFIX}
222
223
17. Enjoy!
224
225
PS: When updating the source from git, it is usually unnecessary to restart Cmake as the solution is able to reconfigure itself when Cmake files are changed. Simply rebuild the solution from Visual Studio and accept the reload of updated projects. It also possible to edit CMakeList.txt files directly in Visual Studio as they also appear in the solution, and projects will be reconfigured on the next generation. To change build options or directory path, it is mandatory to use the Cmake Gui. In case of problems, locate the CMakeCache.txt in "Where to build the binaries” directory and delete it to reconfigure from scratch or use the menu item File->Delete Cache.