Home | History | Annotate | Download | only in core
      1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      2  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      3 
      4 <html xmlns="http://www.w3.org/1999/xhtml">
      5 
      6 <!--
      7   A lot of people read this document template.  Please keep it clean:
      8 
      9    - keep the document xhtml-compliant, as many people use validating editors
     10    - check your edits for typos, spelling errors, and questionable grammar
     11    - prefer css styles to formatting tags like <font>, <tt>, etc.
     12    - keep it human-readable and human-editable in a plain text editor:
     13      - strive to keep lines wrapped at 80 columns, unless a link prevents it
     14      - use plenty of whitespace
     15      - try to pretty-format (wrt nesting and indenting) any hairy html
     16    - check your inline javascript for errors using the javascript console
     17    
     18   Your readers will be very appreciative.
     19 -->
     20 
     21 <head>
     22   <title>Android Build System</title>
     23 
     24   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     25 
     26   <link href="../android.css" type="text/css" rel="stylesheet" />
     27 
     28 <!-- commenting out so the xhtml validator doesn't whine about < and &&;
     29      the browser should still find the script tag. -->
     30 <script language="JavaScript1.2" type="text/javascript">
     31 <!--
     32 function highlight(name) {
     33   if (document.getElementsByTagName) {
     34     tags              = [ 'span', 'div', 'tr', 'td' ];
     35     for (i in tags) {
     36       elements        = document.getElementsByTagName(tags[i]);
     37       if (elements) {
     38         for (j = 0; j < elements.length; j++) {
     39           elementName = elements[j].getAttribute("class");
     40           if (elementName == name) {
     41             elements[j].style.backgroundColor = "#C0F0C0";
     42           } else if (elementName && elementName.indexOf("rev") == 0) {
     43             elements[j].style.backgroundColor = "#FFFFFF";
     44           }
     45         }
     46       }
     47     }
     48   }
     49 }
     50 //-->
     51   </script>
     52   <!-- this style sheet is for the style of the toc -->
     53   <link href="toc.css" type="text/css" rel="stylesheet" />
     54 
     55   <style type="text/css">
     56     .warning {
     57         border: 1px solid red;
     58         padding: 8px;
     59         color: red;
     60     }
     61     pre.prettyprint {
     62         margin-top: 0;
     63     }
     64     li {
     65         margin-top: 8px;
     66     }
     67   </style>
     68 </head>
     69 
     70 <body onload="prettyPrint()">
     71 
     72 <h1><a name="My_Project_" />Android Build System</h1>
     73 
     74 <!-- Status is one of: Draft, Current, Needs Update, Obsolete -->
     75 <p style="text-align:center">
     76   <strong>Status:</strong> <em>Draft </em> &nbsp;
     77   <small>(as of May 18, 2006)</small>
     78 </p>
     79 
     80 <p><b>Contents</b></p>
     81 <!-- this div expands out to a list of contents based on the H2 and H3 headings.
     82 Believe it! -->
     83  <div id="nav"  class="nav-2-levels"></div>
     84 
     85 <h2>Objective</h2>
     86 <p>The primary goals of reworking the build system are (1) to make dependencies
     87 work more reliably, so that when files need to rebuilt, they are, and (2) to
     88 improve performance of the build system so that unnecessary modules are not
     89 rebuilt, and so doing a top-level build when little or nothing needs to be done
     90 for a build takes as little time as possible.</p>
     91 
     92 <h2>Principles and Use Cases and Policy</h2>
     93 <p>Given the above objective, these are the overall principles and use cases
     94 that we will support.  This is not an exhaustive list.</p>
     95 <h3>Multiple Targets</h3>
     96 <p>It needs to be possible to build the Android platform for multiple targets.
     97 This means:</p>
     98 <ul>
     99     <li>The build system will support building tools for the host platform,
    100     both ones that are used in the build process itself, and developer tools
    101     like the simulator.</li>
    102     <li>The build system will need to be able to build tools on Linux
    103     (definitely Goobuntu and maybe Grhat), MacOS, and to some degree on
    104     Windows.</li>
    105     <li>The build system will need to be able to build the OS on Linux, and in
    106     the short-term, MacOS.  Note that this is a conscious decision to stop
    107     building the OS on Windows.  We are going to rely on the emulator there
    108     and not attempt to use the simulator.  This is a requirement change now
    109     that the emulator story is looking brighter.</li>
    110 </ul>
    111 <h3>Non-Recursive Make</h3>
    112 <p>To achieve the objectives, the build system will be rewritten to use make
    113 non-recursively.  For more background on this, read <a href="http://aegis.sourceforge.net/auug97.pdf">Recursive Make Considered Harmful</a>.  For those that don't
    114 want PDF, here is the
    115 <a href="http://72.14.203.104/search?q=cache:HwuX7YF2uBIJ:aegis.sourceforge.net/auug97.pdf&hl=en&gl=us&ct=clnk&cd=2&client=firefox">Google translated version</a>.
    116 <h3>Rapid Compile-Test Cycles</h3>
    117 <p>When developing a component, for example a C++ shared library, it must be
    118 possible to easily rebuild just that component, and not have to wait more than a
    119 couple seconds for dependency checks, and not have to wait for unneeded
    120 components to be built.</p>
    121 <h3>Both Environment and Config File Based Settings</h3>
    122 <p>To set the target, and other options, some people on the team like to have a
    123 configuration file in a directory so they do not have an environment setup
    124 script to run, and others want an environment setup script to run so they can
    125 run builds in different terminals on the same tree, or switch back and forth
    126 in one terminal.  We will support both.</p>
    127 <h3>Object File Directory / make clean</h3>
    128 <p>Object files and other intermediate files will be generated into a directory
    129 that is separate from the source tree.  The goal is to have make clean be
    130 "rm -rf <obj>" in the tree root directory.  The primary goals of
    131 this are to simplify searching the source tree, and to make "make clean" more
    132 reliable.</p>
    133 
    134 <h3>SDK</h3>
    135 <p>The SDK will be a tarball that will allow non-OS-developers to write apps.
    136 The apps will actually be built by first building the SDK, and then building
    137 the apps against that SDK.  This will hopefully (1) make writing apps easier
    138 for us, because we won't have to rebuild the OS as much, and we can use the
    139 standard java-app development tools, and (2) allow us to dog-food the SDK, to
    140 help ensure its quality.  Cedric has suggested (and I agree) that apps built
    141 from the SDK should be built with ant.  Stay tuned for more details as we
    142 figure out exactly how this will work.</p>
    143 
    144 <h3>Dependecies</h3>
    145 <p>Dependencies should all be automatic.  Unless there is a custom tool involved
    146 (e.g. the webkit has several), the dependencies for shared and static libraries,
    147 .c, .cpp, .h, .java, java libraries, etc., should all work without intervention
    148 in the Android.mk file.</p>
    149 
    150 <h3>Hiding command lines</h3>
    151 <p>The default of the build system will be to hide the command lines being
    152 executed for make steps.  It will be possible to override this by specifying
    153 the showcommands pseudo-target, and possibly by setting an environment
    154 variable.</p>
    155 
    156 <h3>Wildcard source files</h3>
    157 <p>Wildcarding source file will be discouraged.  It may be useful in some
    158 scenarios.  The default <code>$(wildcard *)</code> will not work due to the
    159 current directory being set to the root of the build tree.<p>
    160 
    161 <h3>Multiple targets in one directory</h3>
    162 <p>It will be possible to generate more than one target from a given
    163 subdirectory.  For example, libutils generates a shared library for the target
    164 and a static library for the host.</p>
    165 
    166 <h3>Makefile fragments for modules</h3>
    167 <p><b>Android.mk</b> is the standard name for the makefile fragments that
    168 control the building of a given module.  Only the top directory should
    169 have a file named "Makefile".</p>
    170 
    171 <h3>Use shared libraries</h3>
    172 <p>Currently, the simulator is not built to use shared libraries.  This should
    173 be fixed, and now is a good time to do it.  This implies getting shared
    174 libraries to work on Mac OS.</p>
    175 
    176 
    177 <h2>Nice to Have</h2>
    178 
    179 <p>These things would be nice to have, and this is a good place to record them,
    180 however these are not promises.</p>
    181 
    182 <h3>Simultaneous Builds</h3>
    183 <p>The hope is to be able to do two builds for different combos in the same
    184 tree at the same time, but this is a stretch goal, not a requirement.
    185 Doing two builds in the same tree, not at the same time must work.  (update:
    186 it's looking like we'll get the two builds at the same time working)</p>
    187 
    188 <h3>Deleting headers (or other dependecies)</h3>
    189 <p>Problems can arise if you delete a header file that is referenced in
    190 ".d" files.  The easy way to deal with this is "make clean".  There
    191 should be a better way to handle it. (from fadden)</p>
    192 <p>One way of solving this is introducing a dependency on the directory.  The
    193 problem is that this can create extra dependecies and slow down the build.
    194 It's a tradeoff.</p>
    195 
    196 <h3>Multiple builds</h3>
    197 <p>General way to perform builds across the set of known platforms.  This
    198 would make it easy to perform multiple platform builds when testing a
    199 change, and allow a wide-scale "make clean".  Right now the buildspec.mk
    200 or environment variables need to be updated before each build. (from fadden)</p>
    201 
    202 <h3>Aftermarket Locales and Carrier</h3>
    203 <p>We will eventually need to add support for creating locales and carrier
    204 customizations to the SDK, but that will not be addressed right now.</p>
    205 
    206 
    207 <h2><a id="usage"/>Usage</h2>
    208 <p>You've read (or scrolled past) all of the motivations for this build system,
    209 and you want to know how to use it.  This is the place.</p>
    210 
    211 <h3>Your first build</h3>
    212 <p>The <a href="../building.html">Building</a> document describes how do do
    213 builds.</p>
    214 
    215 <h3>build/envsetup.sh functions</h3>
    216 If you source the file build/envsetup.sh into your bash environment,
    217 <code>. build/envsetup.sh</code>you'll get a few helpful shell functions:
    218 
    219 <ul>
    220 <li><b>printconfig</b> - Prints the current configuration as set by the
    221 lunch and choosecombo commands.</li>
    222 <li><b>m</b> - Runs <code>make</code> from the top of the tree.  This is
    223 useful because you can run make from within subdirectories.  If you have the
    224 <code>TOP</code> environment variable set, it uses that.  If you don't, it looks
    225 up the tree from the current directory, trying to find the top of the tree.</li>
    226 <li><b>croot</b> - <code>cd</code> to the top of the tree.</li>
    227 <li><b>sgrep</b> - grep for the regex you provide in all .c, .cpp, .h, .java,
    228 and .xml files below the current directory.</li>
    229 </ul>
    230 
    231 <h3>Build flavors/types</h3>
    232 <p>
    233 When building for a particular product, it's often useful to have minor
    234 variations on what is ultimately the final release build.  These are the
    235 currently-defined "flavors" or "types" (we need to settle on a real name
    236 for these).
    237 </p>
    238 
    239 <table border=1>
    240 <tr>
    241     <td>
    242         <code>eng<code>
    243     </td>
    244     <td>
    245         This is the default flavor. A plain "<code>make</code>" is the
    246         same as "<code>make eng</code>".  <code>droid</code> is an alias
    247         for <code>eng</code>.
    248         <ul>
    249         <li>Installs modules tagged with: <code>eng</code>, <code>debug</code>,
    250             <code>user</code>, and/or <code>development</code>.
    251         <li>Installs non-APK modules that have no tags specified.
    252         <li>Installs APKs according to the product definition files, in
    253             addition to tagged APKs.
    254         <li><code>ro.secure=0</code>
    255         <li><code>ro.debuggable=1</code>
    256         <li><code>ro.kernel.android.checkjni=1</code>
    257         <li><code>adb</code> is enabled by default.
    258     </td>
    259 </tr>
    260 <tr>
    261     <td>
    262         <code>user<code>
    263     </td>
    264     <td>
    265         "<code>make user</code>"
    266         <p>
    267         This is the flavor intended to be the final release bits.
    268         <ul>
    269         <li>Installs modules tagged with <code>user</code>.
    270         <li>Installs non-APK modules that have no tags specified.
    271         <li>Installs APKs according to the product definition files; tags
    272             are ignored for APK modules.
    273         <li><code>ro.secure=1</code>
    274         <li><code>ro.debuggable=0</code>
    275         <li><code>adb</code> is disabled by default.
    276     </td>
    277 </tr>
    278 <tr>
    279     <td>
    280         <code>userdebug<code>
    281     </td>
    282     <td>
    283         "<code>make userdebug</code>"
    284         <p>
    285         The same as <code>user</code>, except:
    286         <ul>
    287         <li>Also installs modules tagged with <code>debug</code>.
    288         <li><code>ro.debuggable=1</code>
    289         <li><code>adb</code> is enabled by default.
    290     </td>
    291 </tr>
    292 </table>
    293 
    294 <p>
    295 If you build one flavor and then want to build another, you should run
    296 "<code>make installclean</code>" between the two makes to guarantee that
    297 you don't pick up files installed by the previous flavor.  "<code>make
    298 clean</code>" will also suffice, but it takes a lot longer.
    299 </p>
    300 
    301 
    302 <h3>More pseudotargets</h3>
    303 <p>Sometimes you want to just build one thing.  The following pseudotargets are
    304 there for your convenience:</p>
    305 
    306 <ul>
    307 <li><b>droid</b> - <code>make droid</code> is the normal build.  This target
    308 is here because the default target has to have a name.</li>
    309 <li><b>all</b> - <code>make all</code> builds everything <code>make
    310 droid</code> does, plus everything whose <code>LOCAL_MODULE_TAGS</code> do not
    311 include the "droid" tag.  The build server runs this to make sure
    312 that everything that is in the tree and has an Android.mk builds.</li>
    313 <li><b>clean-$(LOCAL_MODULE)</b> and <b>clean-$(LOCAL_PACKAGE_NAME)</b> - 
    314 Let you selectively clean one target.  For example, you can type
    315 <code>make clean-libutils</code> and it will delete libutils.so and all of the
    316 intermediate files, or you can type <code>make clean-Home</code> and it will
    317 clean just the Home app.</li>
    318 <li><b>clean</b> - <code>make clean</code> deletes all of the output and
    319 intermediate files for this configuration.  This is the same as <code>rm -rf
    320 out/&lt;configuration&gt;/</code></li>
    321 <li><b>clobber</b> - <code>make clobber</code> deletes all of the output
    322 and intermediate files for all configurations.  This is the same as
    323 <code>rm -rf out/</code>.</li>
    324 <li><b>dataclean</b> - <code>make dataclean</code> deletes contents of the data 
    325 directory inside the current combo directory.  This is especially useful on the
    326 simulator and emulator, where the persistent data remains present between 
    327 builds.</li>
    328 <li><b>showcommands</b> - <code>showcommands</code> is a modifier target
    329 which causes the build system to show the actual command lines for the build
    330 steps, instead of the brief descriptions.  Most people don't like seeing the
    331 actual commands, because they're quite long and hard to read, but if you need
    332 to for debugging purposes, you can add <code>showcommands</code> to the list
    333 of targets you build.  For example <code>make showcommands</code> will build
    334 the default android configuration, and <code>make runtime showcommands</code>
    335 will build just the runtime, and targets that it depends on, while displaying
    336 the full command lines.  Please note that there are a couple places where the
    337 commands aren't shown here.  These are considered bugs, and should be fixed,
    338 but they're often hard to track down.  Please let
    339 <a href="mailto:android-build-team">android-build-team</a> know if you find
    340 any.</li>
    341 <li><b>LOCAL_MODULE</b> - Anything you specify as a <code>LOCAL_MODULE</code>
    342 in an Android.mk is made into a pseudotarget.  For example, <code>make
    343 runtime</code> might be shorthand for <code>make
    344 out/linux-x86-debug/system/bin/runtime</code> (which would work), and
    345 <code>make libkjs</code> might be shorthand for <code>make
    346 out/linux-x86-debug/system/lib/libkjs.so</code> (which would also work).</li>
    347 <li><b>targets</b> - <code>make targets</code> will print a list of all of
    348 the LOCAL_MODULE names you can make.</li>
    349 </ul>
    350 
    351 <h3><a name="templates"/>How to add another component to the build - Android.mk templates</h3>
    352 <p>You have a new library, a new app, or a new executable.  For each of the
    353 common types of modules, there is a corresponding file in the templates
    354 directory.  It will usually be enough to copy one of these, and fill in your
    355 own values.  Some of the more esoteric values are not included in the
    356 templates, but are instead just documented here, as is the documentation
    357 on using custom tools to generate files.</p>
    358 <p>Mostly, you can just look for the TODO comments in the templates and do
    359 what it says.  Please remember to delete the TODO comments when you're done
    360 to keep the files clean.  The templates have minimal documentation in them,
    361 because they're going to be copied, and when that gets stale, the copies just
    362 won't get updated.  So read on...</p>
    363 
    364 <h4>Apps</h4>
    365 <p>Use the <code>templates/apps</code> file.</p>
    366 <p>This template is pretty self-explanitory.  See the variables below for more
    367 details.</p>
    368 
    369 <h4>Java Libraries</h4>
    370 <p>Use the <code>templates/java_library</code> file.</p>
    371 <p>The interesting thing here is the value of LOCAL_MODULE, which becomes
    372 the name of the jar file.  (Actually right now, we're not making jar files yet,
    373 just directories of .class files,  but the directory is named according to
    374 what you put in LOCAL_MODULE).  This name will be what goes in the 
    375 LOCAL_JAVA_LIBRARIES variable in modules that depend on your java library.</p>
    376 
    377 <h4>C/C++ Executables</h4>
    378 <p>Use the <code>templates/executable</code> file, or the
    379 <code>templates/executable_host</code> file.</p>
    380 <p>This template has a couple extra options that you usually don't need.
    381 Please delete the ones you don't need, and remove the TODO comments.  It makes
    382 the rest of them easier to read, and you can always refer back to the templates
    383 if you need them again later.</p>
    384 <p>By default, on the target these are built into /system/bin, and on the
    385 host, they're built into <combo>/host/bin.  These can be overridden by setting
    386 <code>LOCAL_MODULE_PATH</code>.  See
    387 <a href="#moving-targets">Putting targets elsewhere</a>
    388 for more.</p>
    389 
    390 <h4>Shared Libraries</h4>
    391 <p>Use the <code>templates/shared_library</code> file, or the
    392 <code>templates/shared_library_host</code> file.</p>
    393 <p>Remember that on the target, we use shared libraries, and on the host,
    394 we use static libraries, since executable size isn't as big an issue, and it
    395 simplifies distribution in the SDK.</p>
    396 
    397 <h4>Static Libraries</h4>
    398 <p>Use the <code>templates/static_library</code> file, or the
    399 <code>templates/static_library_host</code> file.</p>
    400 <p>Remember that on the target, we use shared libraries, and on the host,
    401 we use static libraries, since executable size isn't as big an issue, and it
    402 simplifies distribution in the SDK.</p>
    403 
    404 <h4><a name="custom-tools"/>Using Custom Tools</h4>
    405 <p>If you have a tool that generates source files for you, it's possible
    406 to have the build system get the dependencies correct for it.  Here are
    407 a couple of examples.  <code>$@</code> is the make built-in variable for
    408 "the current target." The <font color=red>red</font> parts are the parts you'll
    409 need to change.</p>
    410 
    411 <p>You need to put this after you have declared <code>LOCAL_PATH</code> and
    412 <code>LOCAL_MODULE</code>, because the <code>$(local-intermediates-dir)</code>
    413 and <code>$(local-host-intermediates-dir)</code> macros use these variables
    414 to determine where to put the files.
    415 
    416 <h5>Example 1</h5>
    417 <p>Here, there is one generated file, called
    418 chartables.c, which doesn't depend on anything.  And is built by the tool
    419 built to $(HOST_OUT_EXECUTABLES)/dftables.  Note on the second to last line
    420 that a dependency is created on the tool.</p>
    421 <pre>
    422 intermediates:= $(local-intermediates-dir)
    423 GEN := $(intermediates)/<font color=red>chartables.c</font>
    424 $(GEN): PRIVATE_CUSTOM_TOOL = <font color=red>$(HOST_OUT_EXECUTABLES)/dftables $@</font>
    425 $(GEN): <font color=red>$(HOST_OUT_EXECUTABLES)/dftables</font>
    426 	$(transform-generated-source)
    427 LOCAL_GENERATED_SOURCES += $(GEN)
    428 </pre>
    429 
    430 <h5>Example 2</h5>
    431 <p>Here as a hypothetical example, we use use cat as if it were to transform
    432 a file.  Pretend that it does something useful.  Note how we use a
    433 target-specific variable called PRIVATE_INPUT_FILE to store the name of the
    434 input file.</p>
    435 <pre>
    436 intermediates:= $(local-intermediates-dir)
    437 GEN := $(intermediates)/<font color=red>file.c</font>
    438 $(GEN): PRIVATE_INPUT_FILE := $(LOCAL_PATH)/<font color=red>input.file</font>
    439 $(GEN): PRIVATE_CUSTOM_TOOL = <font color=red>cat $(PRIVATE_INPUT_FILE) &gt; $@</font>
    440 $(GEN): <font color=red>$(LOCAL_PATH)/file.c</font>
    441 	$(transform-generated-source)
    442 LOCAL_GENERATED_SOURCES += $(GEN)
    443 </pre>
    444 
    445 <h5>Example 3</h5>
    446 <p>If you have several files that are all similar in
    447 name, and use the same tool, you can combine them.  (here the *.lut.h files are
    448 the generated ones, and the *.cpp files are the input files)</p>
    449 <pre>
    450 intermediates:= $(local-intermediates-dir)
    451 GEN := $(addprefix $(intermediates)<font color=red>/kjs/, \
    452             array_object.lut.h \
    453             bool_object.lut.h \</font>
    454         )
    455 $(GEN): PRIVATE_CUSTOM_TOOL = <font color=red>perl libs/WebKitLib/WebKit/JavaScriptCore/kjs/create_hash_table $< -i > $@</font>
    456 $(GEN): $(intermediates)/<font color=red>%.lut.h</font> : $(LOCAL_PATH)/<font color=red>%.cpp</font>
    457 	$(transform-generated-source)
    458 LOCAL_GENERATED_SOURCES += $(GEN)
    459 </pre>
    460 
    461 <h3><a name="platform-specific"/>Platform specific conditionals</h3>
    462 <p>Sometimes you need to set flags specifically for different platforms.  Here
    463 is a list of which values the different build-system defined variables will be
    464 set to and some examples.</p>
    465 <p>For a device build, <code>TARGET_OS</code> is <code>linux</code> (we're using
    466 linux!), and <code>TARGET_ARCH</code> is <code>arm</code>.</p>
    467 <p>For a simulator build, <code>TARGET_OS</code> and <code>TARGET_ARCH</code>
    468 are set to the same as <code>HOST_OS</code> and <code>HOST_ARCH</code> are
    469 on your platform.  <code>TARGET_PRODUCT</code> is the name of the target
    470 hardware/product you are building for.  The value <code>sim</code> is used
    471 for the simulator.  We haven't thought through the full extent of customization
    472 that will happen here, but likely there will be additional UI configurations
    473 specified here as well.</p>
    474 <table cellspacing=25>
    475 <tr>
    476     <td valign=top align=center>
    477         <b>HOST_OS</b><br/>
    478         linux<br/>
    479         darwin<br/>
    480         (cygwin)
    481     </td>
    482     <td valign=top align=center>
    483         <b>HOST_ARCH</b><br/>
    484         x86
    485     </td>
    486     <td valign=top align=center>
    487         <b>HOST_BUILD_TYPE</b><br/>
    488         release<br/>
    489         debug
    490     </td>
    491 </tr>
    492 <tr>
    493     <td valign=top align=center>
    494         <b>TARGET_OS</b><br/>
    495         linux<br/>
    496         darwin<br/>
    497         (cygwin)
    498     </td>
    499     <td valign=top align=center>
    500         <b>TARGET_ARCH</b><br/>
    501         arm<br/>
    502         x86
    503     </td>
    504     <td valign=top align=center>
    505         <b>TARGET_BUILD_TYPE</b><br/>
    506         release<br/>
    507         debug
    508     </td>
    509     <td valign=top align=center>
    510         <b>TARGET_PRODUCT</b><br/>
    511         sim<br/>
    512         dream<br/>
    513         sooner
    514     </td>
    515 </tr>
    516 </table>
    517 
    518 <h4>TARGET_SIMULATOR</h4>
    519 <p>If we're building the simulator, as opposed to the arm or emulator builds,
    520 <code>TARGET_SIMULATOR</code> will be set to <code>true</code>.
    521 
    522 <h4>Some Examples</h4>
    523 <pre>ifeq ($(TARGET_SIMULATOR),true)
    524 LOCAL_CFLAGS += -DSIMULATOR
    525 endif
    526 
    527 ifeq ($(TARGET_BUILD_TYPE),release)
    528 LOCAL_CFLAGS += -DNDEBUG=1
    529 endif
    530 
    531 # from libutils
    532 ifeq ($(TARGET_OS),linux)
    533 # Use the futex based mutex and condition variable
    534 # implementation from android-arm because it's shared mem safe
    535 LOCAL_SRC_FILES += futex_synchro.c
    536 LOCAL_LDLIBS += -lrt -ldl
    537 endif
    538 
    539 </pre>
    540 
    541 
    542 <h3><a name="moving-modules"/>Putting modules elsewhere</h3>
    543 <p>If you have modules that normally go somewhere, and you need to have them
    544 build somewhere else, read this.  One use of this is putting files on
    545 the root filesystem instead of where they normally go in /system. Add these
    546 lines to your Android.mk:</p>
    547 <pre>
    548 LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
    549 LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
    550 </pre>
    551 <p>For executables and libraries, you need to also specify a
    552 <code>LOCAL_UNSTRIPPED_PATH</code> location, because on target builds, we keep
    553 the unstripped executables so GDB can find the symbols.</code>
    554 <p>Look in <code>config/envsetup.make</code> for all of the variables defining
    555 places to build things.</p>
    556 <p>FYI: If you're installing an executable to /sbin, you probably also want to
    557 set <code>LOCAL_FORCE_STATIC_EXCUTABLE := true</code> in your Android.mk, which
    558 will force the linker to only accept static libraries.</p>
    559 
    560 
    561 <h3>Android.mk variables</h3>
    562 <p>These are the variables that you'll commonly see in Android.mk files, listed
    563 alphabetically.</p>
    564 <p>But first, a note on variable naming:
    565 <ul>
    566     <li><b>LOCAL_</b> - These variables are set per-module.  They are cleared
    567     by the <code>include $(CLEAR_VARS)</code> line, so you can rely on them
    568     being empty after including that file.  Most of the variables you'll use
    569     in most modules are LOCAL_ variables.</li>
    570     <li><b>PRIVATE_</b> - These variables are make-target-specific variables.  That
    571     means they're only usable within the commands for that module.  It also
    572     means that they're unlikely to change behind your back from modules that
    573     are included after yours.  This 
    574     <a href="http://www.gnu.org/software/make/manual/make.html#Target_002dspecific">link to the make documentation</a>
    575     describes more about target-specific variables.  Please note that there
    576     are a couple of these laying around the tree that aren't prefixed with
    577     PRIVATE_.  It is safe, and they will be fixed as they are discovered.
    578     Sorry for the confusion.</li>
    579     <li><b>INTERNAL_</b> - These variables are critical to functioning of
    580     the build system, so you shouldn't create variables named like this, and
    581     you probably shouldn't be messing with these variables in your makefiles.
    582     </li>
    583     <li><b>HOST_</b> and <b>TARGET_</b> - These contain the directories
    584     and definitions that are specific to either the host or the target builds.
    585     Do not set variables that start with HOST_ or TARGET_ in your makefiles.
    586     </li>
    587     <li><b>BUILD_</b> and <b>CLEAR_VARS</b> - These contain the names of
    588     well-defined template makefiles to include.  Some examples are CLEAR_VARS
    589     and BUILD_HOST_PACKAGE.</li>
    590     <li>Any other name is fair-game for you to use in your Android.mk.  However,
    591     remember that this is a non-recursive build system, so it is possible that
    592     your variable will be changed by another Android.mk included later, and be
    593     different when the commands for your rule / module are executed.</li>
    594 </ul>
    595 </p>
    596 
    597 <h4>LOCAL_ASSET_FILES</h4>
    598 <p>In Android.mk files that <code>include $(BUILD_PACKAGE)</code> set this
    599 to the set of files you want built into your app.  Usually:</p>
    600 <p><code>LOCAL_ASSET_FILES += $(call find-subdir-assets)</code></p>
    601 <p>This will probably change when we switch to ant for the apps' build
    602 system.</p>
    603 
    604 <h4>LOCAL_CC</h4>
    605 <p>If you want to use a different C compiler for this module, set LOCAL_CC
    606 to the path to the compiler.  If LOCAL_CC is blank, the appropriate default
    607 compiler is used.</p>
    608 
    609 <h4>LOCAL_CXX</h4>
    610 <p>If you want to use a different C++ compiler for this module, set LOCAL_CXX
    611 to the path to the compiler.  If LOCAL_CXX is blank, the appropriate default
    612 compiler is used.</p>
    613 
    614 <h4>LOCAL_CFLAGS</h4>
    615 <p>If you have additional flags to pass into the C or C++ compiler, add
    616 them here.  For example:</p>
    617 <p><code>LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1</code></p>
    618 
    619 <h4>LOCAL_CPPFLAGS</h4>
    620 <p>If you have additional flags to pass into <i>only</i> the C++ compiler, add
    621 them here.  For example:</p>
    622 <p><code>LOCAL_CPPFLAGS += -ffriend-injection</code></p>
    623 <code>LOCAL_CPPFLAGS</code> is guaranteed to be after <code>LOCAL_CFLAGS</code>
    624 on the compile line, so you can use it to override flags listed in
    625 <code>LOCAL_CFLAGS</code>.
    626 
    627 <h4>LOCAL_CPP_EXTENSION</h4>
    628 <p>If your C++ files end in something other than "<code>.cpp</code>",
    629 you can specify the custom extension here.  For example:</p>
    630 <p><code>LOCAL_CPP_EXTENSION := .cc</code></p>
    631 Note that all C++ files for a given module must have the same
    632 extension; it is not currently possible to mix different extensions.
    633 
    634 <h4>LOCAL_NO_DEFAULT_COMPILER_FLAGS</h4>
    635 <p>Normally, the compile line for C and C++ files includes global include
    636 paths and global cflags.  If <code>LOCAL_NO_DEFAULT_COMPILER_FLAGS</code>
    637 is non-empty, none of the default includes or flags will be used when compiling
    638 C and C++ files in this module.
    639 <code>LOCAL_C_INCLUDES</code>, <code>LOCAL_CFLAGS</code>, and
    640 <code>LOCAL_CPPFLAGS</code> will still be used in this case, as will
    641 any <code>DEBUG_CFLAGS</code> that are defined for the module.
    642 
    643 <h4>LOCAL_COPY_HEADERS</h4>
    644 <p class=warning>This will be going away.</p>
    645 <p>The set of files to copy to the install include tree.  You must also
    646 supply <code>LOCAL_COPY_HEADERS_TO</code>.</p>
    647 <p>This is going away because copying headers messes up the error messages, and
    648 may lead to people editing those headers instead of the correct ones.  It also
    649 makes it easier to do bad layering in the system, which we want to avoid.  We
    650 also aren't doing a C/C++ SDK, so there is no ultimate requirement to copy any
    651 headers.</p>
    652 
    653 <h4>LOCAL_COPY_HEADERS_TO</h4>
    654 <p class=warning>This will be going away.</p>
    655 <p>The directory within "include" to copy the headers listed in
    656 <code>LOCAL_COPY_HEADERS</code> to.</p>
    657 <p>This is going away because copying headers messes up the error messages, and
    658 may lead to people editing those headers instead of the correct ones.  It also
    659 makes it easier to do bad layering in the system, which we want to avoid.  We
    660 also aren't doing a C/C++ SDK, so there is no ultimate requirement to copy any
    661 headers.</p>
    662 
    663 <h4>LOCAL_C_INCLUDES</h4>
    664 <p>Additional directories to instruct the C/C++ compilers to look for header
    665 files in.  These paths are rooted at the top of the tree.  Use
    666 <code>LOCAL_PATH</code> if you have subdirectories of your own that you
    667 want in the include paths.  For example:</p>
    668 <p><code>
    669 LOCAL_C_INCLUDES += extlibs/zlib-1.2.3<br/>
    670 LOCAL_C_INCLUDES += $(LOCAL_PATH)/src
    671 </code></p>
    672 <p>You should not add subdirectories of include to
    673 <code>LOCAL_C_INCLUDES</code>, instead you should reference those files
    674 in the <code>#include</code> statement with their subdirectories.  For
    675 example:</p>
    676 <p><code>#include &lt;utils/KeyedVector.h&gt;</code><br/>
    677 not <code><s>#include &lt;KeyedVector.h&gt;</s></code></p>
    678 <p>There are some components that are doing this wrong, and should be cleaned
    679 up.</p>
    680 
    681 <h4>LOCAL_MODULE_TAGS</h4>
    682 <p>Set <code>LOCAL_MODULE_TAGS</code> to any number of whitespace-separated
    683 tags.  If the tag list is empty or contains <code>droid</code>, the module
    684 will get installed as part of a <code>make droid</code>.  Otherwise, it will
    685 only get installed by running <code>make &lt;your-module&gt;</code>
    686 or with the <code>make all</code> pseudotarget.</p>
    687 
    688 <h4>LOCAL_REQUIRED_MODULES</h4>
    689 <p>Set <code>LOCAL_REQUIRED_MODULES</code> to any number of whitespace-separated
    690 module names, like "libblah" or "Email".  If this module is installed, all
    691 of the modules that it requires will be installed as well.  This can be
    692 used to, e.g., ensure that necessary shared libraries or providers are
    693 installed when a given app is installed.
    694 
    695 <h4>LOCAL_FORCE_STATIC_EXECUTABLE</h4>
    696 <p>If your executable should be linked statically, set 
    697 <code>LOCAL_FORCE_STATIC_EXECUTABLE:=true</code>.  There is a very short
    698 list of libraries that we have in static form (currently only libc).  This is
    699 really only used for executables in /sbin on the root filesystem.</p>
    700 
    701 <h4>LOCAL_GENERATED_SOURCES</h4>
    702 <p>Files that you add to <code>LOCAL_GENERATED_SOURCES</code> will be
    703 automatically generated and then linked in when your module is built.
    704 See the <a href="#custom-tools">Custom Tools</a> template makefile for an
    705 example.</p>
    706 
    707 <h4>LOCAL_JAVACFLAGS</h4>
    708 <p>If you have additional flags to pass into the javac compiler, add
    709 them here.  For example:</p>
    710 <p><code>LOCAL_JAVACFLAGS += -Xlint:deprecation</code></p>
    711 
    712 <h4>LOCAL_JAVA_LIBRARIES</h4>
    713 <p>When linking Java apps and libraries, <code>LOCAL_JAVA_LIBRARIES</code>
    714 specifies which sets of java classes to include.  Currently there are
    715 two of these: <code>core</code> and <code>framework</code>.
    716 In most cases, it will look like this:</p>
    717 <p><code>LOCAL_JAVA_LIBRARIES := core framework</code></p>
    718 <p>Note that setting <code>LOCAL_JAVA_LIBRARIES</code> is not necessary
    719 (and is not allowed) when building an APK with
    720 "<code>include $(BUILD_PACKAGE)</code>".  The appropriate libraries
    721 will be included automatically.</p>
    722 
    723 <h4>LOCAL_LDFLAGS</h4>
    724 <p>You can pass additional flags to the linker by setting
    725 <code>LOCAL_LDFLAGS</code>.  Keep in mind that the order of parameters is
    726 very important to ld, so test whatever you do on all platforms.</p>
    727 
    728 <h4>LOCAL_LDLIBS</h4>
    729 <p><code>LOCAL_LDLIBS</code> allows you to specify additional libraries
    730 that are not part of the build for your executable or library.  Specify
    731 the libraries you want in -lxxx format; they're passed directly to the 
    732 link line.  However, keep in mind that there will be no dependency generated
    733 for these libraries.  It's most useful in simulator builds where you want
    734 to use a library preinstalled on the host.  The linker (ld) is a particularly
    735 fussy beast, so it's sometimes necessary to pass other flags here if you're
    736 doing something sneaky. Some examples:</p>
    737 <p><code>LOCAL_LDLIBS += -lcurses -lpthread<br/>
    738 LOCAL_LDLIBS += -Wl,-z,origin
    739 </code></p>
    740 
    741 <h4>LOCAL_NO_MANIFEST</h4>
    742 <p>If your package doesn't have a manifest (AndroidManifest.xml), then
    743 set <code>LOCAL_NO_MANIFEST:=true</code>.  The common resources package
    744 does this.</p>
    745 
    746 <h4>LOCAL_PACKAGE_NAME</h4>
    747 <p><code>LOCAL_PACKAGE_NAME</code> is the name of an app.  For example,
    748 Dialer, Contacts, etc.  This will probably change or go away when we switch
    749 to an ant-based build system for the apps.</p>
    750 
    751 <h4>LOCAL_PATH</h4>
    752 <p>The directory your Android.mk file is in. You can set it by putting the
    753 following as the first line in your Android.mk:</p>
    754 <p><code>LOCAL_PATH := $(my-dir)</code></p>
    755 <p>The <code>my-dir</code> macro uses the 
    756 <code><a href="http://www.gnu.org/software/make/manual/make.html#MAKEFILE_005fLIST-Variable">MAKEFILE_LIST</a></code>
    757 variable, so you must call it before you include any other makefiles.  Also,
    758 consider that any subdirectories you inlcude might reset LOCAL_PATH, so do your
    759 own stuff before you include them.  This also means that if you try to write
    760 several <code>include</code> lines that reference <code>LOCAL_PATH</code>,
    761 it won't work, because those included makefiles might reset LOCAL_PATH.
    762 
    763 <h4>LOCAL_POST_PROCESS_COMMAND</h4>
    764 <p>For host executables, you can specify a command to run on the module
    765 after it's been linked.  You might have to go through some contortions
    766 to get variables right because of early or late variable evaluation:</p>
    767 <p><code>module := $(HOST_OUT_EXECUTABLES)/$(LOCAL_MODULE)<br/>
    768 LOCAL_POST_PROCESS_COMMAND := /Developer/Tools/Rez -d __DARWIN__ -t APPL\<br/>
    769 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-d __WXMAC__ -o $(module) Carbon.r
    770 </code></p>
    771 
    772 <h4>LOCAL_PREBUILT_EXECUTABLES</h4>
    773 <p>When including $(BUILD_PREBUILT) or $(BUILD_HOST_PREBUILT), set these to
    774 executables that you want copied.  They're located automatically into the
    775 right bin directory.</p>
    776 
    777 <h4>LOCAL_PREBUILT_LIBS</h4>
    778 <p>When including $(BUILD_PREBUILT) or $(BUILD_HOST_PREBUILT), set these to
    779 libraries that you want copied.  They're located automatically into the
    780 right lib directory.</p>
    781 
    782 <h4>LOCAL_SHARED_LIBRARIES</h4>
    783 <p>These are the libraries you directly link against.  You don't need to
    784 pass transitively included libraries.  Specify the name without the suffix:</p>
    785 <p><code>LOCAL_SHARED_LIBRARIES := \<br/>
    786 	&nbsp;&nbsp;&nbsp;&nbsp;libutils \<br/>
    787 	&nbsp;&nbsp;&nbsp;&nbsp;libui \<br/>
    788 	&nbsp;&nbsp;&nbsp;&nbsp;libaudio \<br/>
    789 	&nbsp;&nbsp;&nbsp;&nbsp;libexpat \<br/>
    790 	&nbsp;&nbsp;&nbsp;&nbsp;libsgl
    791 </code></p>
    792 
    793 <h4>LOCAL_SRC_FILES</h4>
    794 <p>The build system looks at <code>LOCAL_SRC_FILES</code> to know what source
    795 files to compile -- .cpp .c .y .l .java.  For lex and yacc files, it knows
    796 how to correctly do the intermediate .h and .c/.cpp files automatically.  If
    797 the files are in a subdirectory of the one containing the Android.mk, prefix
    798 them with the directory name:</p>
    799 <p><code>LOCAL_SRC_FILES := \<br/>
    800 	&nbsp;&nbsp;&nbsp;&nbsp;file1.cpp \<br/>
    801 	&nbsp;&nbsp;&nbsp;&nbsp;dir/file2.cpp
    802 </code></p>
    803 
    804 <h4>LOCAL_STATIC_LIBRARIES</h4>
    805 <p>These are the static libraries that you want to include in your module.
    806 Mostly, we use shared libraries, but there are a couple of places, like
    807 executables in sbin and host executables where we use static libraries instead.
    808 <p><code>LOCAL_STATIC_LIBRARIES := \<br/>
    809 	&nbsp;&nbsp;&nbsp;&nbsp;libutils \<br/>
    810 	&nbsp;&nbsp;&nbsp;&nbsp;libtinyxml
    811 </code></p>
    812 
    813 <h4>LOCAL_MODULE</h4>
    814 <p><code>LOCAL_MODULE</code> is the name of what's supposed to be generated
    815 from your Android.mk.  For exmample, for libkjs, the <code>LOCAL_MODULE</code>
    816 is "libkjs" (the build system adds the appropriate suffix -- .so .dylib .dll).
    817 For app modules, use <code>LOCAL_PACKAGE_NAME</code> instead of 
    818 <code>LOCAL_MODULE</code>.  We're planning on switching to ant for the apps,
    819 so this might become moot.</p>
    820 
    821 <h4>LOCAL_MODULE_PATH</h4>
    822 <p>Instructs the build system to put the module somewhere other than what's
    823 normal for its type.  If you override this, make sure you also set
    824 <code>LOCAL_UNSTRIPPED_PATH</code> if it's an executable or a shared library
    825 so the unstripped binary has somewhere to go.  An error will occur if you forget
    826 to.</p>
    827 <p>See <a href="#moving-modules">Putting modules elsewhere</a> for more.</p>
    828 
    829 <h4>LOCAL_UNSTRIPPED_PATH</h4>
    830 <p>Instructs the build system to put the unstripped version of the module
    831 somewhere other than what's normal for its type.  Usually, you override this
    832 because you overrode <code>LOCAL_MODULE_PATH</code> for an executable or a
    833 shared library.  If you overrode <code>LOCAL_MODULE_PATH</code>, but not 
    834 <code>LOCAL_UNSTRIPPED_PATH</code>, an error will occur.</p>
    835 <p>See <a href="#moving-modules">Putting modules elsewhere</a> for more.</p>
    836 
    837 <h4>LOCAL_WHOLE_STATIC_LIBRARIES</h4>
    838 <p>These are the static libraries that you want to include in your module without allowing
    839 the linker to remove dead code from them. This is mostly useful if you want to add a static library
    840 to a shared library and have the static library's content exposed from the shared library.
    841 <p><code>LOCAL_WHOLE_STATIC_LIBRARIES := \<br/>
    842 	&nbsp;&nbsp;&nbsp;&nbsp;libsqlite3_android<br/>
    843 </code></p>
    844 
    845 <h4>LOCAL_YACCFLAGS</h4>
    846 <p>Any flags to pass to invocations of yacc for your module.  A known limitation
    847 here is that the flags will be the same for all invocations of YACC for your
    848 module.  This can be fixed.  If you ever need it to be, just ask.</p>
    849 <p><code>LOCAL_YACCFLAGS := -p kjsyy</code></p>
    850 
    851 
    852 
    853 <h2>Implementation Details</h2>
    854 
    855 <p>You should never have to touch anything in the config directory unless
    856 you're adding a new platform, new tools, or adding new features to the
    857 build system.  In general, please consult with the build system owner(s)
    858 (<a href="mailto:android-build-team">android-build-team</a>) before you go
    859 mucking around in here.  That said, here are some notes on what's going on
    860 under the hood.</p>
    861 
    862 <h3>Environment Setup / buildspec.mk Versioning</h3>
    863 <p>In order to make easier for people when the build system changes, when
    864 it is necessary to make changes to buildspec.mk or to rerun the environment
    865 setup scripts, they contain a version number in the variable
    866 BUILD_ENV_SEQUENCE_NUMBER.  If this variable does not match what the build
    867 system expects, it fails printing an error message explaining what happened.
    868 If you make a change that requires an update, you need to update two places
    869 so this message will be printed.
    870 <ul>
    871     <li>In config/envsetup.make, increment the
    872         CORRECT_BUILD_ENV_SEQUENCE_NUMBER definition.</li>
    873     <li>In buildspec.mk.default, update the BUILD_ENV_SEQUENCE_DUMBER
    874         definition to match the one in config/envsetup.make</li>
    875 </ul>
    876 The scripts automatically get the value from the build system, so they will
    877 trigger the warning as well.
    878 </p>
    879 
    880 <h3>Additional makefile variables</h3>
    881 <p>You probably shouldn't use these variables.  Please consult
    882 <a href="mailto:android-build-team">android-build-team</a> before using them.
    883 These are mostly there for workarounds for other issues, or things that aren't
    884 completely done right.</p>
    885 
    886 <h4>LOCAL_ADDITIONAL_DEPENDENCIES</h4>
    887 <p>If your module needs to depend on anything else that
    888 isn't actually built in to it, you can add those make targets to 
    889 <code>LOCAL_ADDITIONAL_DEPENDENCIES</code>.  Usually this is a workaround
    890 for some other dependency that isn't created automatically.</p>
    891 
    892 <h4>LOCAL_BUILT_MODULE</h4>
    893 <p>When a module is built, the module is created in an intermediate
    894 directory then copied to its final location.  LOCAL_BUILT_MODULE is
    895 the full path to the intermediate file.  See LOCAL_INSTALLED_MODULE
    896 for the path to the final installed location of the module.</p>
    897 
    898 <h4>LOCAL_HOST</h4>
    899 <p>Set by the host_xxx.make includes to tell base_rules.make and the other
    900 includes that we're building for the host.  Kenneth did this as part of
    901 openbinder, and I would like to clean it up so the rules, includes and
    902 definitions aren't duplicated for host and target.</p>
    903 
    904 <h4>LOCAL_INSTALLED_MODULE</h4>
    905 <p>The fully qualified path name of the final location of the module.
    906 See LOCAL_BUILT_MODULE for the location of the intermediate file that
    907 the make rules should actually be constructing.</p>
    908 
    909 <h4>LOCAL_REPLACE_VARS</h4>
    910 <p>Used in some stuff remaining from the openbinder for building scripts
    911 with particular values set,</p>
    912 
    913 <h4>LOCAL_SCRIPTS</h4>
    914 <p>Used in some stuff remaining from the openbinder build system that we
    915 might find handy some day.</p>
    916 
    917 <h4>LOCAL_MODULE_CLASS</h4>
    918 <p>Which kind of module this is.  This variable is used to construct other
    919 variable names used to locate the modules.  See base_rules.make and
    920 envsetup.make.</p>
    921 
    922 <h4>LOCAL_MODULE_NAME</h4>
    923 <p>Set to the leaf name of the LOCAL_BUILT_MODULE.  I'm not sure,
    924 but it looks like it's just used in the WHO_AM_I variable to identify
    925 in the pretty printing what's being built.</p>
    926 
    927 <h4>LOCAL_MODULE_SUFFIX</h4>
    928 <p>The suffix that will be appended to <code>LOCAL_MODULE</code> to form
    929 <code>LOCAL_MODULE_NAME</code>.  For example, .so, .a, .dylib.</p>
    930 
    931 <h4>LOCAL_STRIP_MODULE</h4>
    932 <p>Calculated in base_rules.make to determine if this module should actually
    933 be stripped or not, based on whether <code>LOCAL_STRIPPABLE_MODULE</code>
    934 is set, and whether the combo is configured to ever strip modules.  With
    935 Iliyan's stripping tool, this might change.</p>
    936 
    937 <h4>LOCAL_STRIPPABLE_MODULE</h4>
    938 <p>Set by the include makefiles if that type of module is strippable. 
    939 Executables and shared libraries are.</p>
    940 
    941 <h4>LOCAL_SYSTEM_SHARED_LIBRARIES</h4>
    942 <p>Used while building the base libraries: libc, libm, libdl.  Usually
    943 it should be set to "none," as it is in $(CLEAR_VARS).  When building
    944 these libraries, it's set to the ones they link against.  For example,
    945 libc, libstdc++ and libdl don't link against anything, and libm links against
    946 libc.  Normally, when the value is none, these libraries are automatically
    947 linked in to executables and libraries, so you don't need to specify them
    948 manually.</p>
    949 
    950 
    951 </body>
    952 </html>
    953