1 page.title=Android.mk 2 @jd:body 3 4 <div id="qv-wrapper"> 5 <div id="qv"> 6 <h2>On this page</h2> 7 8 <ol> 9 <li><a href="#over">Overview</a></li> 10 <li><a href="#basics">Basics</a></li> 11 <li><a href="#var">Variables and Macros</a></li> 12 <li><a href="#mdv">Module-Description Variables</a></li> 13 </ol> 14 </div> 15 </div> 16 17 18 <p>This page describes the syntax of the {@code Android.mk} build file, 19 which glues your C and C++ source files to the Android NDK.</p> 20 21 <h2 id="over">Overview</h2> 22 <p>The {@code Android.mk} file resides in a subdirectory of your project's {@code jni/} directory, 23 and describes your sources and shared libraries to the build system. It is really a tiny GNU 24 makefile fragment that the build system parses once or more. The {@code Android.mk} file is useful 25 for defining project-wide settings that <a href="{@docRoot}ndk/guides/application_mk.html">{@code 26 Application.mk}</a>, the build system, and your 27 environment variables leave undefined. It can also override project-wide settings for specific 28 <i>modules</i>.</p> 29 30 <p>The syntax of the {@code Android.mk} allows you to group your sources into 31 <em>modules</em>. A module is either a static library, a shared library, or a standalone 32 executable. You can define one or more modules in each {@code Android.mk} file, and 33 you can use the same source file in multiple modules. The build system only places shared libraries 34 into your application package. In addition, static libraries can generate shared libraries.</p> 35 36 <p>In addition to packaging libraries, the build system handles a variety of other details for you. 37 For example, you don't need to list header files or explicit dependencies between generated files in 38 your {@code Android.mk} file. The NDK build system computes these relationships automatically for 39 you. As a result, you should be able to benefit from new toolchain/platform support in future NDK 40 releases without having to touch your {@code Android.mk} file.</p> 41 42 <p>The syntax of this file is very close to that used in the {@code Android.mk} files distributed with 43 the full <a href="https://source.android.com">Android Open Source Project</a>. While the 44 build system implementation that uses them is different, their similarity is an 45 intentional design decision aimed at making it easier for application 46 developers to reuse source code for external libraries.</p> 47 48 <h2 id="basics">Basics</h2> 49 <p>Before exploring the syntax in detail, it is useful to start by understanding the basics 50 of what a {@code Android.mk} file contains. This section uses the {@code Android.mk} file in the 51 Hello-JNI sample toward that end, explaining the role that each line in the file plays.</p> 52 53 54 <p>An {@code Android.mk} file must begin by defining the {@code LOCAL_PATH} variable: 55 56 <pre class="no-pretty-print"> 57 LOCAL_PATH := $(call my-dir) 58 </pre> 59 60 <p>This variable indicates the location of the source files in the development tree. Here, the macro 61 function {@code my-dir}, provided by the build system, returns the path of the current directory 62 (the directory containing the {@code Android.mk} file itself).</p> 63 64 <p>The next line declares the {@code CLEAR_VARS} variable, whose value the build system provides. 65 66 <pre class="no-pretty-print"> 67 include $(CLEAR_VARS) 68 </pre> 69 70 <p>The {@code CLEAR_VARS} variable points to a special GNU Makefile that clears many 71 {@code LOCAL_XXX} variables for you, such as {@code LOCAL_MODULE}, {@code LOCAL_SRC_FILES}, and 72 {@code LOCAL_STATIC_LIBRARIES}. Note that it does not clear {@code LOCAL_PATH}. This variable must 73 retain its value because the system parses all build control files in a single GNU Make execution 74 context where all variables are global. You must (re-)declare this variable before describing each 75 module.</p> 76 77 <p>Next, the {@code LOCAL_MODULE} variable stores the name of the module that you wish to build. 78 Use this variable once per module in your application.</p> 79 80 <pre class="no-pretty-print"> 81 LOCAL_MODULE := hello-jni 82 </pre> 83 84 <p>Each module name must be unique and not contain any spaces. The build system, when it 85 generates the final shared-library file, automatically adds the proper prefix and suffix to 86 the name that you assign to {@code LOCAL_MODULE}. For example, the example that appears above 87 results in generation of a library called {@code libhello-jni.so}.</p> 88 89 <p class="note"><strong>Note:</strong> If your module's name already starts with {@code lib}, the 90 build system does not prepend an additional {@code lib} prefix; it takes the module name as-is, and 91 adds the {@code .so} extension. So a source file originally called, for example, {@code libfoo.c} 92 still produces a shared-object file called {@code libfoo.so}. This behavior is to support libraries 93 that the Android platform sources generate from {@code Android.mk} files; the names of all such 94 libraries start with {@code lib}.</p> 95 96 <p>The next line enumerates the source files, with spaces delimiting multiple files:</p> 97 98 <pre class="no-pretty-print"> 99 LOCAL_SRC_FILES := hello-jni.c 100 </pre> 101 102 <p>The {@code LOCAL_SRC_FILES} variable must contain a list of C and/or C++ source files to build 103 into a module.</p> 104 105 <p>The last line helps the system tie everything together:</p> 106 107 <pre class="no-pretty-print"> 108 include $(BUILD_SHARED_LIBRARY) 109 </pre> 110 111 <p>The {@code BUILD_SHARED_LIBRARY} variable points to a GNU Makefile script that collects all the 112 information you defined in {@code LOCAL_XXX} variables since the most recent {@code include}. This 113 script determines what to build, and how to do it.</p> 114 115 <p>There are more complex examples in the samples directories, with commented 116 {@code Android.mk} files that you can look at. In addition, 117 <a href="{@docRoot}ndk/samples/sample_na.html">Sample: native-activity</a> provides 118 a detailed explanation of that sample's {@code Android.mk} file. Finally, <a href="#var"> 119 Variables and Macros</a> provides further information on the variables from this section. 120 121 122 <h2 id="var">Variables and Macros</h2> 123 <p>The build system provides many possible variables for use in the the {@code Android.mk} file. 124 Many of these variables come with preassigned values. Others, you assign.</p> 125 126 <p>In addition to these variables, you can also define your own arbitrary ones. If you do so, keep 127 in mind that the NDK build system reserves the following variable names:</p> 128 <ul> 129 <li>Names that begin with {@code LOCAL_}, such as {@code LOCAL_MODULE}.</li> 130 <li>Names that begin with {@code PRIVATE_}, {@code NDK_}, or {@code APP}. The build system uses 131 these internally.</li> 132 <li>Lower-case names, such as {@code my-dir}. The build system uses these internally, as well.</li> 133 </ul> 134 <p>If you need to define your own convenience variables in an {@code Android.mk} file, we 135 recommend prepending {@code MY_} to their names. 136 137 138 <h3 id="npv">NDK-defined variables</h3> 139 <p>This section discusses the GNU Make variables that the build system defines before parsing your 140 {@code Android.mk} file. Under certain circumstances, the NDK might parse your {@code Android.mk} 141 file several times, using a different definition for some of these variables each time.</p> 142 143 <h4>CLEAR_VARS</h4> 144 <p>This variable points to a build script that undefines nearly all {@code LOCAL_XXX} variables 145 listed in the "Developer-defined variables" section below. Use this variable to include 146 this script before describing a new module. The syntax for using it is:</p> 147 148 <pre class="no-pretty-print"> 149 include $(CLEAR_VARS) 150 </pre> 151 152 <h4>BUILD_SHARED_LIBRARY</h4> 153 <p>This variable points to a build script that collects all the information about the module 154 you provided in your {@code LOCAL_XXX} variables, and determines how to build a target shared 155 library from the sources you listed. Note that using this script requires that you have already 156 assigned values to {@code LOCAL_MODULE} and {@code LOCAL_SRC_FILES}, at a minimum (for more 157 information about these variables, see <a href = "#mdv">Module-Description Variables</a>).</p> 158 159 <p>The syntax for using this variable is:</p> 160 161 <pre class="no-pretty-print"> 162 include $(BUILD_SHARED_LIBRARY) 163 </pre> 164 165 <p>A shared-library variable causes the build system to generate a library file with a {@code .so} 166 extension.</p> 167 168 <h4>BUILD_STATIC_LIBRARY</h4> 169 <p>A variant of {@code BUILD_SHARED_LIBRARY} that is used to build a static library. The build 170 system does not copy static libraries into your project/packages, but it can use them to build 171 shared libraries (see {@code LOCAL_STATIC_LIBRARIES} and {@code LOCAL_WHOLE_STATIC_LIBRARIES}, 172 below). The syntax for using this variable is:</p> 173 174 <pre class="no-pretty-print"> 175 include $(BUILD_STATIC_LIBRARY) 176 </pre> 177 178 <p>A static-library variable causes the build system to generate a library with a {@code .a} 179 extension.</p> 180 181 <h4>PREBUILT_SHARED_LIBRARY</h4> 182 <p>Points to a build script used to specify a prebuilt shared library. Unlike in the case of 183 {@code BUILD_SHARED_LIBRARY} and {@code BUILD_STATIC_LIBRARY}, here the value of 184 {@code LOCAL_SRC_FILES} cannot be a source file. Instead, it must be a single path to a prebuilt 185 shared library, such as {@code foo/libfoo.so}. The syntax for using this variable is:</p> 186 187 <pre class="no-pretty-print"> 188 include $(PREBUILT_SHARED_LIBRARY) 189 </pre> 190 191 <p>You can also reference a prebuilt library in another module by using the 192 {@code LOCAL_PREBUILTS} variable. For more information about using prebuilts, see 193 <a href="{@docRoot}ndk/guides/prebuilts.html">Using Prebuilt Libraries</a>.</p> 194 195 196 <h4>PREBUILT_STATIC_LIBRARY</h4> 197 <p>The same as {@code PREBUILT_SHARED_LIBRARY}, but for a prebuilt static library. For more 198 information about using prebuilts, see <a href="{@docRoot}ndk/guides/prebuilts.html">Using Prebuilt 199 Libraries</a>.</p> 200 201 <h4>TARGET_ARCH</h4> 202 <p>The name of the target CPU architecture as the Android Open Source Project specifies it. 203 For any ARM-compatible build, use {@code arm}, independent of the CPU architecture revision or 204 ABI (see TARGET_ARCH_ABI, below).</p> 205 206 <p>The value of this variable is taken from the APP_ABI variable that you define in the 207 {@code Android.mk} file, which the system reads ahead of parsing the {@code Android.mk} file.</p> 208 209 <h4>TARGET_PLATFORM</h4> 210 <p>The Android API level number for the build system to target. 211 For example, the Android 5.1 system images correspond to Android API level 22: {@code android-22}. 212 For a complete list of platform names and corresponding Android system 213 images, see <a href="{@docRoot}ndk/guides/stable_apis.html">Android NDK Native APIs</a>. 214 The following example shows the syntax for using this variable:</p> 215 216 <pre class="no-pretty-print"> 217 TARGET_PLATFORM := android-22 218 </pre> 219 220 <h4 id="taa">TARGET_ARCH_ABI</h4> 221 <p>This variable stores the name of the CPU and architecture to target when the build system 222 parses this {@code Android.mk} file. You can specify one or more of the following values, using 223 a space as a delimiter between multiple targets. Table 1 shows the ABI setting to use for each 224 supported CPU and architecture. 225 226 <p class="table-caption" id="table1"> 227 <strong>Table 1.</strong> ABI settings for different CPUs and architectures.</p> 228 <table> 229 <tr> 230 <th scope="col">CPU and architecture</th> 231 <th scope="col">Setting</th> 232 </tr> 233 <tr> 234 <td>ARMv5TE</td> 235 <td>{@code armeabi}</td> 236 </tr> 237 <tr> 238 <td>ARMv7</td> 239 <td>{@code armeabi-v7a}</td> 240 </tr> 241 <tr> 242 <td>ARMv8 AArch64</td> 243 <td>{@code arm64-v8a}</td> 244 </tr> 245 <tr> 246 <td>i686</td> 247 <td>{@code x86}</td> 248 </tr> 249 <tr> 250 <td>x86-64</td> 251 <td>{@code x86_64}</td> 252 </tr> 253 <tr> 254 <td>mips32 (r1)</td> 255 <td>{@code mips}</td> 256 </tr> 257 <tr> 258 <td>mips64 (r6)</td> 259 <td>{@code mips64}</td> 260 </tr> 261 <tr> 262 <td>All</td> 263 <td>{@code all}</td> 264 </tr> 265 </table> 266 267 <p>The following example shows how to set ARMv8 AArch64 as the target CPU-and-ABI combination:</p> 268 269 <pre class="no-pretty-print"> 270 TARGET_ARCH_ABI := arm64-v8a 271 </pre> 272 273 <p class="note"><strong>Note: </strong> Up to Android NDK 1.6_r1, this variable is defined as 274 {@code arm}.</p> 275 276 <p>For more details about architecture ABIs and associated compatibility 277 issues, refer to 278 <a href="{@docRoot}ndk/guides/abis.html">ABI Management</a>.</p> 279 280 <p>New target ABIs in the future will have different values.</p> 281 282 <h4>TARGET_ABI</h4> 283 <p>A concatenation of target Android API level and ABI, it is especially useful when you want to test against 284 a specific target system image for a real device. For example, to specify a 64-bit ARM device 285 running on Android API level 22:</p> 286 287 <pre class="no-pretty-print"> 288 TARGET_ABI := android-22-arm64-v8a 289 </pre> 290 291 <p class="note"><strong>Note:</strong> Up to Android NDK 1.6_r1, the default value was 292 {@code android-3-arm}.</p> 293 294 <h2 id="mdv">Module-Description Variables</h2> 295 <p>The variables in this section describe your module to the build system. Each module description 296 should follow this basic flow: 297 <ul> 298 <ol type = "1"> 299 <li>Initialize or undefine the variables associated with the module, using the {@code CLEAR_VARS} 300 variable.</li> 301 <li>Assign values to the variables used to describe the module. 302 <li>Set the NDK build system to use the appropriate build script for the module, using the 303 {@code BUILD_XXX} variable.</li> 304 </ol> 305 </ul> 306 307 <h4>LOCAL_PATH</h4> 308 <p>This variable is used to give the path of the current file. You must define 309 it at the start of your {@code Android.mk} file. The following example shows how to do so:</p> 310 311 <pre class="no-pretty-print"> 312 LOCAL_PATH := $(call my-dir) 313 </pre> 314 315 <p>The script to which {@code CLEAR_VARS} points does not clear this variable. Therefore, you only need 316 to define it a single time, even if your {@code Android.mk} file describes multiple modules.</p> 317 318 <h4>LOCAL_MODULE</h4> 319 <p>This variable stores the name of your module. It must be unique among all module names, 320 and must not contain any spaces. You must define it before including any scripts (other than 321 the one for {@code CLEAR_VARS}). You need not add either the {@code lib} prefix 322 or the {@code .so} or {@code .a} file extension; the build system makes these modifications 323 automatically. Throughout your {@code Android.mk} and 324 <a href="{@docRoot}ndk/guides/application_mk.html">{@code Application.mk}</a> files, refer to 325 your module by its unmodified name. For example, the following line results in the generation of a 326 shared library module called {@code libfoo.so}:</p> 327 328 <pre class="no-pretty-print"> 329 LOCAL_MODULE := "foo" 330 </pre> 331 332 <p>If you want the generated module to have a name other than {@code lib} + the value of 333 {@code LOCAL_MODULE}, you can use the {@code LOCAL_MODULE_FILENAME} variable to give the 334 generated module a name of your own choosing, instead.</p> 335 336 <h4>LOCAL_MODULE_FILENAME</h4> 337 <p>This optional variable allows you to override the names that the build system 338 uses by default for files that it generates. For example, if the name of your {@code LOCAL_MODULE} 339 is {@code foo}, you can force the system to call the file it generates {@code libnewfoo}. The 340 following example shows how to accomplish this:</p> 341 342 <pre class="no-pretty-print"> 343 LOCAL_MODULE := foo 344 LOCAL_MODULE_FILENAME := libnewfoo 345 </pre> 346 347 <p>For a shared library module, this example would generate a file called {@code libnewfoo.so}.</p> 348 349 <p class="note"><strong>Note:</strong> You cannot override filepath or file extension.</p> 350 351 <h4>LOCAL_SRC_FILES</h4> 352 <p>This variable contains the list of source files that the build system uses to generate the 353 module. Only list the files that the build system actually passes to the compiler, since the build 354 system automatically computes any associated depencies.</p> 355 <p>Note that you can use both relative (to {@code LOCAL_PATH}) and absolute file paths. 356 357 <p>We recommend avoiding absolute file paths; relative paths make your {@code Android.mk} file more 358 portable.</p> 359 360 <p class="note"><strong>Note: </strong> Always use Unix-style forward slashes (/) in build files. 361 The build system does not handle Windows-style backslashes (\) properly.</p> 362 363 <h4>LOCAL_CPP_EXTENSION</h4> 364 <p>You can use this optional variable to indicate a file extension other than {@code .cpp} for your 365 C++ source files. For example, the following line changes the extension to {@code .cxx}. 366 (The setting must include the dot.) 367 368 <pre class="no-pretty-print"> 369 LOCAL_CPP_EXTENSION := .cxx 370 </pre> 371 372 <p>From NDK r7, you can use this variable to specify multiple extensions. For instance:</p> 373 374 <pre class="no-pretty-print"> 375 LOCAL_CPP_EXTENSION := .cxx .cpp .cc 376 </pre> 377 378 <h4>LOCAL_CPP_FEATURES</h4> 379 380 <p>You can use this optional variable to indicate that your code relies on specific C++ features. 381 It enables the right compiler and linker flags during the build process. For prebuilt binaries, 382 this variable also declares which features the binary depends on, thus helping ensure the final 383 linking works correctly. We recommend that you use this variable instead of enabling 384 {@code -frtti} and {@code -fexceptions} directly in your {@code LOCAL_CPPFLAGS} definition.</p> 385 386 <p>Using this variable allows the build system to use the appropriate flags for each module. Using 387 {@code LOCAL_CPPFLAGS} causes the compiler to use all specified flags for all modules, regardless 388 of actual need.</p> 389 390 For example, to indicate that your code uses RTTI (RunTime Type Information), write: </p> 391 392 <pre class="no-pretty-print"> 393 LOCAL_CPP_FEATURES := rtti 394 </pre> 395 396 <p>To indicate that your code uses C++ exceptions, write:</p> 397 398 <pre class="no-pretty-print"> 399 LOCAL_CPP_FEATURES := exceptions 400 </pre> 401 402 <p>You can also specify multiple values for this variable. For example:</p> 403 404 <pre class="no-pretty-print"> 405 LOCAL_CPP_FEATURES := rtti features 406 </pre> 407 408 The order in which you describe the values does not matter. 409 410 411 <h4>LOCAL_C_INCLUDES</h4> 412 <p>You can use this optional variable to specify a list of paths, relative to the 413 NDK {@code root} directory, to add to the include search path when compiling all sources 414 (C, C++ and Assembly). For example: </p> 415 416 <pre class="no-pretty-print"> 417 LOCAL_C_INCLUDES := sources/foo 418 </pre> 419 420 <p>Or even: </p> 421 422 <pre class="no-pretty-print"> 423 LOCAL_C_INCLUDES := $(LOCAL_PATH)/<subdirectory>/foo 424 </pre> 425 426 <p>Define this variable before setting any corresponding inclusion flags via {@code LOCAL_CFLAGS} 427 or {@code LOCAL_CPPFLAGS}.</p> 428 429 <p>The build system also uses {@code LOCAL_C_INCLUDES} paths automatically when launching native 430 debugging with ndk-gdb.</p> 431 432 433 <h4>LOCAL_CFLAGS</h4> 434 435 <p>This optional variable sets compiler flags for the build system to pass when building C 436 <em>and</em> C++ source files. The ability to do so can be useful for specifying additional macro 437 definitions or compile options.</p> 438 439 <p>Try not to change the optimization/debugging level in your {@code Android.mk} file. 440 The build system can handle this setting automatically for you, using the relevant information 441 in the <a href="{@docRoot}ndk/guides/application_mk.html">{@code Application.mk}</a> file. Doing it 442 this way allows the build system to generate useful data files used during debugging.</p> 443 444 <p class="note"><strong>Note: </strong>In android-ndk-1.5_r1, the corresponding flags only applied 445 to C source files, not C++ ones. They now match the full Android build system behavior. 446 (You can now use {@code LOCAL_CPPFLAGS} to specify flags for C++ sources only.)</p> 447 448 <p>It is possible to specify additional include paths by writing: 449 450 <pre class="no-pretty-print"> 451 LOCAL_CFLAGS += -I<path>, 452 </pre> 453 454 It is better, however, to use {@code LOCAL_C_INCLUDES} for this purpose, since 455 doing so also makes it possible to use the paths available for native debugging with ndk-gdb.</p> 456 457 458 <h4>LOCAL_CPPFLAGS</h4> 459 <p>An optional set of compiler flags that will be passed when building C++ 460 source files <em>only</em>. They will appear after the LOCAL_CFLAGS on the 461 compiler's command-line.</p> 462 463 464 <p class="note"><strong>Note: </strong>In android-ndk-1.5_r1, the corresponding flags applied to 465 both C and C++ sources. This has been corrected to match the full Android build system. 466 To specify flags for both C and C++ sources, use {@code LOCAL_CFLAGS}.</p> 467 468 469 <h4>LOCAL_STATIC_LIBRARIES</h4> 470 471 <p>This variable stores the list of static libraries modules on which the current module depends.</p> 472 473 <p>If the current module is a shared library or an executable, this variable will force 474 these libraries to be linked into the resulting binary.</p> 475 476 <p>If the current module is a static library, this variable simply indicates that other 477 modules depending on the current one will also depend on the listed 478 libraries.</p> 479 480 <h4>LOCAL_SHARED_LIBRARIES</h4> 481 482 <p>This variable is the list of shared libraries <em>modules</em> on which this module depends at 483 runtime. This information is necessary at link time, and to embed the corresponding information 484 in the generated file.</p> 485 486 <h4>LOCAL_WHOLE_STATIC_LIBRARIES</h4> 487 <p>This variable is a variant of {@code LOCAL_STATIC_LIBRARIES}, and expresses that the linker 488 should treat the associated library modules as <em>whole archives</em>. For more information 489 on whole archives, see the GNU linker's 490 <a href="http://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_3.html">documentation</a> for the 491 {@code --whole-archive} flag.</p> 492 493 <p>This variable is useful when there are circular dependencies among 494 several static libraries. When you use this variable to build a shared library, it will force 495 the build system to add all object files from your static libraries to the final binary. The same 496 is not true, however, when generating executables.</p> 497 498 499 <h4>LOCAL_LDLIBS</h4> 500 501 <p>This variable contains the list of additional linker flags for use in building your shared 502 library or executable. It enables you to use the {@code -l} prefix to pass the name of specific 503 system libraries. For example, the following example tells the linker to generate a module that 504 links to {@code /system/lib/libz.so} at load time: </p> 505 506 <pre class="no-pretty-print"> 507 LOCAL_LDLIBS := -lz 508 </pre> 509 510 <p>For the list of exposed system libraries against which you can link in this NDK release, see 511 <a href="stable_apis.html">Android NDK Native APIs</a>.</p> 512 513 <p class="note"><strong>Note: </strong> If you define this variable for a static library, 514 the build system ignores it, and {@code ndk-build} prints a warning.</p> 515 516 <h4>LOCAL_LDFLAGS</h4> 517 518 <p>The list of other linker flags for the build system to use when building your shared library 519 or executable. For example, the following example uses the {@code ld.bfd} linker on ARM/X86 GCC 520 4.6+, on which {@code ld.gold} is the default </p> 521 522 <pre class="no-pretty-print"> 523 LOCAL_LDFLAGS += -fuse-ld=bfd 524 </pre> 525 526 <p class="note"><strong>Note: </strong>If you define this variable for a static library, the build 527 system ignores it, and ndk-build prints a warning.</p> 528 529 <h4>LOCAL_ALLOW_UNDEFINED_SYMBOLS</h4> 530 531 <p>By default, when the build system encounters an undefined reference encountered while trying to 532 build a shared, it will throw an <em>undefined symbol</em> error. This error can help you catch 533 catch bugs in your source code.</p> 534 535 <p>To disable this check, set this variable to {@code true}. Note that this setting may cause the 536 shared library to load at runtime.</p> 537 538 <p class="note"><strong>Note: </strong> If you define this variable for a static library, 539 the build system ignores it, and ndk-build prints a warning.</p> 540 541 <h4>LOCAL_ARM_MODE</h4 542 > 543 <p>By default, the build system generates ARM target binaries in <em>thumb</em> mode, where each 544 instruction is 16 bits wide and linked with the STL libraries in the {@code thumb/} directory. 545 Defining this variable as {@code arm} forces the build system to generate the module's object 546 files in 32-bit {@code arm} mode. The following example shows how to do this:</p> 547 548 <pre class="no-pretty-print"> 549 LOCAL_ARM_MODE := arm 550 </pre> 551 552 <p>You can also instruct the build system to only build specific sources in {@code arm} mode by 553 appending {@code .arm} suffix to the the source filenames. For example, the following example 554 tells the build system to always compile {@code bar.c} in ARM mode, but to build 555 {@code foo.c} according to the value of {@code LOCAL_ARM_MODE}.</p> 556 557 <pre class="no-pretty-print"> 558 LOCAL_SRC_FILES := foo.c bar.c.arm 559 </pre> 560 561 <p></p> 562 563 <p class="note"><strong>Note: </strong> You can also force the build system to generate ARM binaries 564 by setting {@code APP_OPTIM} in your 565 <a href="{@docRoot}ndk/guides/application_mk.html">{@code Application.mk}</a> file to {@code debug}. 566 Specifying {@code debug} forces an ARM build because the toolchain debugger does not handle Thumb 567 code properly.</p> 568 569 570 <h4>LOCAL_ARM_NEON</h4> 571 <p>This variable only matters when you are targeting the {@code armeabi-v7a} ABI. It allows the 572 use of ARM Advanced SIMD (NEON) GCC intrinsics in your C and C++ sources, as well as NEON 573 instructions in Assembly files.</p> 574 575 <p>Note that not all ARMv7-based CPUs support the NEON instruction set extensions. For this reason, 576 you must perform runtime detection to be able to safely use this code at runtime. For more 577 information, see <a href="{@docRoot}ndk/guides/cpu-arm-neon.html">NEON Support</a> and <a 578 href="{@docRoot}ndk/guides/cpu-features.html">The {@code cpufeatures} Library</a>.</p> 579 580 <p>Alternatively, you can use the {@code .neon} suffix to specify that the build system only 581 compile specific source files with NEON support. In the following example, the build system compiles 582 {@code foo.c} with thumb and neon support, {@code bar.c} with thumb support, and 583 {@code zoo.c} with support for ARM and NEON:</p> 584 585 <pre class="no-pretty-print"> 586 LOCAL_SRC_FILES = foo.c.neon bar.c zoo.c.arm.neon 587 </pre> 588 589 590 <p>If you use both suffixes, {@code .arm} must precede {@code .neon}.</p> 591 592 <h4>LOCAL_DISABLE_NO_EXECUTE</h4> 593 594 <p>Android NDK r4 added support for the "NX bit" security feature. It is 595 enabled by default, but you can disable it by setting this variable to {@code true}. We do not 596 recommend doing so without a compelling reason.</p> 597 598 <p>This feature does not modify the ABI, and is only enabled on kernels 599 targeting ARMv6+ CPU devices. Machine code with this feature enabled 600 will run unmodified on devices running earlier CPU architectures.</p> 601 <p>For more information, see <a href="http://en.wikipedia.org/wiki/NX_bit">Wikipedia: NX bit</a> 602 and <a href="http://www.gentoo.org/proj/en/hardened/gnu-stack.xml">The GNU stack kickstart</a>. 603 604 <h4>LOCAL_DISABLE_RELRO</h4> 605 606 <p>By default, the NDK compiles code with read-only relocations and GOT 607 protection. This variable instructs the runtime linker to mark certain regions of memory 608 as read-only after relocation, making certain security exploits (such as GOT overwrites) 609 more difficult. Note that these protections are only effective on Android API level 16 and higher. 610 On lower API levels, the code will still run, but without memory protections.</p> 611 612 <p>This variable is turned on by default, but you can disable it by setting its value to 613 {@code true}. We do not recommend doing so without a compelling reason.</p> 614 615 <p>For more information, see 616 <a href="http://isisblogs.poly.edu/2011/06/01/relro-relocation-read-only/">RELRO: 617 RELocation Read-Only</a> and <a href="http://www.akkadia.org/drepper/nonselsec.pdf">Security 618 enhancements in RedHat Enterprise Linux (section 6)</a>.</p> 619 620 <h4>LOCAL_DISABLE_FORMAT_STRING_CHECKS</h4> 621 622 <p>By default, the build system compiles code with format string protection. Doing so forces a 623 compiler error if a non-constant format string is used in a {@code printf}-style function.</p> 624 <p>This protection is on by default, but you can disable it by setting the value of 625 this variable to {@code true}. We do not recommend doing so without a compelling reason.</p> 626 627 628 <h4>LOCAL_EXPORT_CFLAGS</h4> 629 630 <p>This variable records a set of C/C++ compiler flags to add to the {@code LOCAL_CFLAGS} definition 631 of any other module that uses this one via the {@code LOCAL_STATIC_LIBRARIES} or 632 {@code LOCAL_SHARED_LIBRARIES} variables.</p> 633 634 <p>For example, consider the following pair of modules: {@code foo} and {@code bar}, which depends 635 on {@code foo}:</p> 636 637 <pre class="no-pretty-print"> 638 include $(CLEAR_VARS) 639 LOCAL_MODULE := foo 640 LOCAL_SRC_FILES := foo/foo.c 641 LOCAL_EXPORT_CFLAGS := -DFOO=1 642 include $(BUILD_STATIC_LIBRARY) 643 644 645 include $(CLEAR_VARS) 646 LOCAL_MODULE := bar 647 LOCAL_SRC_FILES := bar.c 648 LOCAL_CFLAGS := -DBAR=2 649 LOCAL_STATIC_LIBRARIES := foo 650 include $(BUILD_SHARED_LIBRARY) 651 </pre> 652 653 <p>Here, the build system passes the flags {@code -DFOO=1} and {@code -DBAR=2} to the compiler when 654 building {@code bar.c}. It also prepends exported flags to your your module's {@code LOCAL_CFLAGS} 655 so you can easily override them.</p> 656 657 In addition, the relationship among modules is transitive: If {@code zoo} depends on 658 {@code bar}, which in turn depends on {@code foo}, then {@code zoo} also inherits all flags 659 exported from {@code foo}.</p> 660 661 <p>Finally, the build system does not use exported flags when building locally (i.e., building the 662 module whose flags it is exporting). Thus, in the example above, it does not pass {@code -DFOO=1} 663 to the compiler when building {@code foo/foo.c}. To build locally, use {@code LOCAL_CFLAGS} 664 instead.</p> 665 666 <h4>LOCAL_EXPORT_CPPFLAGS</h4> 667 <p>This variable is the same as {@code LOCAL_EXPORT_CFLAGS}, but for C++ flags only.</p> 668 669 <h4>LOCAL_EXPORT_C_INCLUDES</h4> 670 <p>This variable is the same as {@code LOCAL_EXPORT_CFLAGS}, but for C include paths. It is useful 671 in cases where, for example, {@code bar.c} needs to include headers from module {@code foo}.</p> 672 673 <h4>LOCAL_EXPORT_LDFLAGS</h4> 674 <p>This variable is the same as {@code LOCAL_EXPORT_CFLAGS}, but for linker flags.</p> 675 676 <h4>LOCAL_EXPORT_LDLIBS</h4> 677 <p>This variable is the same as {@code LOCAL_EXPORT_CFLAGS}, telling the build system to pass names 678 of specific system libraries to the compiler. Prepend {@code -l} to the name of each library you 679 specify.</p> 680 681 <p>Note that the build system appends imported linker flags to the value of your module's 682 {@code LOCAL_LDLIBS} variable. It does this due to the way Unix linkers work.</p> 683 684 <p>This variable is typically useful when module {@code foo} is a static library 685 and has code that depends on a system library. You can then use {@code LOCAL_EXPORT_LDLIBS} to 686 to export the dependency. For example: </p> 687 688 <pre class="no-pretty-print"> 689 include $(CLEAR_VARS) 690 LOCAL_MODULE := foo 691 LOCAL_SRC_FILES := foo/foo.c 692 LOCAL_EXPORT_LDLIBS := -llog 693 include $(BUILD_STATIC_LIBRARY) 694 695 include $(CLEAR_VARS) 696 LOCAL_MODULE := bar 697 LOCAL_SRC_FILES := bar.c 698 LOCAL_STATIC_LIBRARIES := foo 699 include $(BUILD_SHARED_LIBRARY) 700 </pre> 701 702 <p>In this example, the build system puts {@code -llog} at the end of the linker command when it 703 builds {@code libbar.so}. Doing so tells the linker that, because {@code libbar.so} depends 704 on {@code foo}, it also depends on the system logging library.</p> 705 706 <h4>LOCAL_SHORT_COMMANDS</h4> 707 <p>Set this variable to {@code true} when your module has a very high 708 number of sources and/or dependent static or shared libraries. Doing so forces the 709 build system to use {@code @} syntax for archives containing intermediate object files 710 or linking libraries.</p> 711 712 <p>This feature can be useful on Windows, where the command line accepts a maximum of only 713 of 8191 characters, which can be too small for complex projects. It also impacts the compilation of 714 individual source files, placing nearly all compiler flags inside list files, too.</p> 715 716 <p>Note that any value other than {@code true} will revert to the 717 default behaviour. You can also define {@code APP_SHORT_COMMANDS} in your 718 <a href="{@docRoot}ndk/guides/application_mk.html">{@code Application.mk}</a> file to force this 719 behavior for all modules in your project.</p> 720 721 <p>We do not recommend enabling this feature by default, since it makes the build slower.</p> 722 723 724 <h4>LOCAL_THIN_ARCHIVE</h4> 725 726 <p>Set this variable to {@code true} when building static libraries. 727 Doing so will generate a <strong>thin archive</strong>, a library file that does not contain 728 object files, but instead just file paths to the actual objects that it would normally 729 contain.</p> 730 <p>This is useful to reduce the size of your build output. The drawback is that 731 such libraries <em>cannot</em> be moved to a different location (all paths 732 inside them are relative).</p> 733 <p>Valid values are {@code true}, {@code false} or empty. A 734 default value can be set in your <a href="{@docRoot}ndk/guides/application_mk.html"> 735 {@code Application.mk}</a> file through the {@code APP_THIN_ARCHIVE} 736 737 variable.</p> 738 <p class="note"><strong>Note:</strong> This is ignored for non-static library modules, or prebuilt 739 static library ones.</p> 740 741 <h4>LOCAL_FILTER_ASM</h4> 742 <p>Define this variable as a shell command that the build system will use to filter the 743 assembly files extracted or generated from the files you specified for {@code LOCAL_SRC_FILES}.</p> 744 <p>Defining this variable causes the following things to occur:</p> 745 746 <ul> 747 <ol type = "1"> 748 <li>The build system generates a temporary assembly file from any C or C++ source file, instead of compiling them into an object file.</li> 749 <li>The build system executes the shell command in {@code LOCAL_FILTER_ASM} 750 on any temporary assembly file and on any assembly file 751 listed in {@code LOCAL_SRC_FILES}, thus generating another temporary assembly 752 file.</li> 753 <li>The build system compiles these filtered assembly files into an object file.</li> 754 </ol> 755 </ul> 756 <p>For example:</p> 757 758 <pre class="no-pretty-print"> 759 LOCAL_SRC_FILES := foo.c bar.S 760 LOCAL_FILTER_ASM := 761 762 foo.c --1--> $OBJS_DIR/foo.S.original --2--> $OBJS_DIR/foo.S --3--> $OBJS_DIR/foo.o 763 bar.S --2--> $OBJS_DIR/bar.S --3--> $OBJS_DIR/bar.o 764 </pre> 765 766 <p>"1" corresponds to the compiler, "2" to the filter, and "3" to the assembler. The filter must 767 be a standalone shell command that takes the name of the input file as its first argument, and the 768 name of the output file as the second one. For example:</p> 769 770 <pre class="no-pretty-print"> 771 myasmfilter $OBJS_DIR/foo.S.original $OBJS_DIR/foo.S 772 myasmfilter bar.S $OBJS_DIR/bar.S 773 </pre> 774 775 <h3 id="npfm">NDK-provided function macros</h2> 776 <p>This section explains GNU Make function macros that the NDK provides. Use 777 {@code $(call <function>)} to evaluate them; they return textual information.</p> 778 779 <h4>my-dir</h4> 780 781 <p>This macro returns the path of the last included makefile, which typically is the 782 current {@code Android.mk}'s directory. {@code my-dir} is useful for defining 783 {@code LOCAL_PATH} at the start of your {@code Android.mk} file. For example:</p> 784 785 <pre class="no-pretty-print"> 786 LOCAL_PATH := $(call my-dir) 787 </pre> 788 789 <p>Due to the way GNU Make works, what this macro really returns is the 790 path of the last makefile that the build system included when parsing the build scripts. For this 791 reason, you should not call {@code my-dir} after including another file.</p> 792 793 <p>For example, consider the following example: </p> 794 795 <pre class="no-pretty-print"> 796 LOCAL_PATH := $(call my-dir) 797 798 # ... declare one module 799 800 include $(LOCAL_PATH)/foo/`Android.mk` 801 802 LOCAL_PATH := $(call my-dir) 803 804 # ... declare another module 805 </pre> 806 807 <p>The problem here is that the second call to {@code my-dir} defines 808 {@code LOCAL_PATH} as {@code $PATH/foo} instead of {@code $PATH}, because that was where its 809 most recent include pointed.</p> 810 811 <p>You can avoid this problem by putting additional includes after everything 812 else in the {@code Android.mk} file. For example:</p> 813 814 <pre class="no-pretty-print"> 815 LOCAL_PATH := $(call my-dir) 816 817 # ... declare one module 818 819 LOCAL_PATH := $(call my-dir) 820 821 # ... declare another module 822 823 # extra includes at the end of the Android.mk file 824 include $(LOCAL_PATH)/foo/Android.mk 825 826 </pre> 827 828 <p>If it is not feasible to structure the file in this way, save the value of the first 829 {@code my-dir} call into another variable. For example: </p> 830 831 <pre class="no-pretty-print"> 832 MY_LOCAL_PATH := $(call my-dir) 833 834 LOCAL_PATH := $(MY_LOCAL_PATH) 835 836 # ... declare one module 837 838 include $(LOCAL_PATH)/foo/`Android.mk` 839 840 LOCAL_PATH := $(MY_LOCAL_PATH) 841 842 # ... declare another module 843 </pre> 844 845 <h4>all-subdir-makefiles</h4> 846 847 <p>Returns the list of {@code Android.mk} files located in all subdirectories of 848 the current {@code my-dir} path. 849 850 <p>You can use this function to provide deep-nested source directory hierarchies to the build 851 system. By default, the NDK only looks for files in the directory containing the 852 {@code Android.mk} file.</p> 853 854 <h4>this-makefile</h4> 855 <p>Returns the path of the current makefile (from which the build system called the function).</p> 856 857 <h4>parent-makefile</h4> 858 <p>Returns the path of the parent makefile in the inclusion tree (the path of the makefile that 859 included the current one).</p> 860 861 <h4>grand-parent-makefile</h4> 862 <p>Returns the path of the grandparent makefile in the inclusion tree (the path of the makefile that 863 included the current one).</p> 864 865 <h4>import-module</h4> 866 <p>A function that allows you to find and include a module's {@code Android.mk} file by the name of 867 the module. A typical example is as follows: </p> 868 869 <pre class="no-pretty-print"> 870 $(call import-module,<name>) 871 </pre> 872 873 <p>In this example, the build system looks for the module tagged {@code <name>} in the list of 874 directories referenced that your {@code NDK_MODULE_PATH} environment variable references, and 875 includes its {@code Android.mk} file automatically for you.</p>