Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: native_client_sdk/src/doc/devguide/devcycle/building.rst

Issue 23463019: [NaCl Docs] Add devcyle/building.rst (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 .. _devcycle-building: 1 .. _devcycle-building:
2 2
3 ######## 3 ########
4 Building 4 Building
5 ######## 5 ########
6 6
7 foo 7 .. contents:: Table Of Contents
8 :local:
9 :backlinks: none
10 :depth: 2
11
12
13 Introduction
14 ============
15
16 This document describes how to build Native Client modules. It is intended for
17 developers who have experience writing, compiling, and linking C and C++ code.
18 If you haven't read the Native Client :doc:`Technical Overview
19 <../../overview>` and :doc:`Tutorial <../tutorial>`, we recommend starting
20 with those.
21
22 Target architectures
23 --------------------
24
25 Native Client modules are written in C or C++ and compiled into executable
26 files ending in a .nexe extension using one of the toolchains in the Native
27 Client SDK. Chrome can load .nexe files embedded in web pages and execute the
28 .nexe files as part of a web application.
29
30 As explained in the Technical Overview, Native Client modules are
31 operating-system-independent, not processor-independent. Therefore, you must
32 compile separate versions of your Native Client module for different processors
33 on end-user machines, such as x86-32, x86-64, and ARM. The list below shows
34 which .nexe modules run on which target architectures:
35
36 **x86 32-bit .nexe modules run on**:
37
38 * Windows 32-bit
39 * Mac
40 * Linux 32-bit
41 * Linux 64-bit (but only with 32-bit Chrome)
42
43 **x86 64-bit .nexe modules run on**:
44
45 * Windows 64-bit
46 * Linux 64-bit (but only with 64-bit Chrome).
47
48 **ARM .nexe modules run on**:
49
50 * ARM devices
51
52 In general, you must compile a module for the 32-bit and 64-bit x86 target
53 architectures (we also strongly recommend compiling modules for the ARM
54 architecture), and create a :ref:`manifest file <application_files>` that
55 specifies which version of the module to load based on the end-user's
56 architecture. The SDK includes a script, ``create_nmf.py`` (in the ``tools/``
57 directory) to generate manifest files. For examples of how to compile modules
58 for multiple target architectures and how to generate manifest files, see the
59 Makefiles included with the SDK examples.
60
61 C libraries
62 -----------
63
64 The Native Client SDK comes with two C libraries: `newlib
65 <http://sourceware.org/newlib/>`_ and `glibc
66 <http://www.gnu.org/software/libc/>`_. See :doc:`Dynamic Linking & Loading with
67 glibc <dynamic-loading>` for information about these libraries, including
68 factors to help you decide which to use.
69
70 SDK toolchains
71 --------------
72
73 The Native Client SDK includes multiple toolchains, differentiated by target
74 architectures and C libraries. The toolchains are located in directories named
75 ``toolchain/<platform>_<architecture>_<library>``, where:
76
77 * *<platform>* is the platform of your development machine (win, mac, or linux)
78 * *<architecture>* is your target architecture (x86 or arm)
79 * *<library>* is the C library you are compiling with (newlib or glibc)
80
81 The compilers, linkers, and other tools are located in the bin/ subdirectory in
82 each toolchain. For example, the tools in the Windows SDK for the x86 target
83 architecture using the newlib library are in ``toolchain/win_x86_newlib/bin``.
84
85 .. Note::
86 :class: note
87
88 The SDK toolchains descend from the ``toolchain/`` directory. The SDK also
89 has a ``tools/`` directory; this directory contains utilities that are not
90 properly part of the toolchains but that you may find helpful in building and
91 testing your application (e.g., the ``create_nmf.py`` script, which you can
92 use to create a manifest file).
93
94 SDK toolchains versus your hosted toolchain
95 -------------------------------------------
96
97 To build .nexe files, you must use one of the Native Client toolchains included
98 in the SDK. The SDK toolchains use a variety of techniques to ensure that your
99 ``.nexe`` files comply with the security constraints of the Native Client
100 sandbox.
101
102 During development, you have another choice: You can build modules using a
103 standard GNU toolchain, such as the hosted toolchain on your development
104 machine. The benefit of using a standard GNU toolchain is that you can develop
105 modules in your favorite IDE and use your favorite debugging and profiling
106 tools. The drawback is that modules compiled in this manner can only run as
107 Pepper plugins in Chrome. To publish and distribute Native Client modules as
108 part of a web application, you must eventually use a toolchain in the Native
109 Client SDK to create ``.nexe`` files.
110
111 .. Note::
112 :class: note
113
114 * Documentation on how to compile and run modules as Pepper plugins will be
115 published soon.
116 * In the future, additional tools will be available to compile Native Client
117 modules written in other programming languages, such as C#. But this
118 document covers only compiling C and C++ code, using the modified GNU
119 toolchains provided in the SDK.
120
121 Tools
122 =====
123
124 The Native Client toolchains contain modified versions of the tools in the
125 standard GNU toolchain, including the gcc compilers (currently version 4.4) and
126 the linkers and other tools from binutils (currently version 2.20).
127
128 In the toolchain for the ARM target architecture, each tool's name is preceded
129 by the prefix "arm-nacl-".
130
131 In the toolchains for the x86 target architecture, there are actually two
132 versions of each tool—one to build Native Client modules for the x86-32 target
133 architecture, and one to build modules for the x86-64 target architecture. Each
134 tool's name is preceded by one of the following prefixes:
135
136 i686-nacl-
137 prefix for tools used to build 32-bit .nexes
138
139 x86_64-nacl-
140 prefix for tools used to build 64-bit .nexes
141
142 These prefixes conform to gcc naming standards and make it easy to use tools
143 like autoconf. As an example, you can use ``i686-nacl-gcc`` to compile 32-bit
144 .nexes, and ``x86_64-nacl-gcc`` to compile 64-bit .nexes. Note that you can
145 typically override a tool's default target architecture with command line
146 flags, e.g., you can specify ``x86_64-nacl-gcc -m32`` to compile a 32-bit
147 .nexe.
148
149 The SDK toolchains include the following tools:
150
151 * <prefix>addr2line
152 * <prefix>ar
153 * <prefix>as
154 * <prefix>c++
155 * <prefix>c++filt
156 * <prefix>cpp
157 * <prefix>g++
158 * <prefix>gcc
159 * <prefix>gcc-4.4.3
160 * <prefix>gccbug
161 * <prefix>gcov
162 * <prefix>gprof
163 * <prefix>ld
164 * <prefix>nm
165 * <prefix>objcopy
166 * <prefix>objdump
167 * <prefix>ranlib
168 * <prefix>readelf
169 * <prefix>size
170 * <prefix>strings
171 * <prefix>strip
172
173 Compiling
174 =========
175
176 To create a .nexe file, use a compiler in one of the Native Client toolchains.
177
178 For example, assuming you're developing on a Windows machine, targeting the x86
179 architecture, and using the newlib library, you can compile a 32-bit .nexe for
180 the hello_world example with the following command::
181
182 <NACL_SDK_ROOT>/toolchain/win_x86_newlib/bin/i686-nacl-gcc hello_world.c ^
183 -o hello_world_x86_32.nexe -m32 -g -O0 -lppapi
184
185 (The carat ``^`` allows the command to span multiple lines on Windows; to do the
186 same on Mac and Linux use a back slash instead. Or you can simply type the
187 command and all its arguments on one line. ``<NACL_SDK_ROOT>`` represents the
188 path to the top-level directory of the bundle you are using, e.g.,
189 ``<location-where-you-installed-the-SDK>/pepper_28``.)
190
191 To compile a 64-bit .nexe, you can run the same command but use -m64 instead of
192 -m32. Alternatively, you could also use the version of the compiler that
193 targets the x86-64 architecture, i.e., ``x86_64-nacl-gcc``.
194
195 You should name executable modules with a ``.nexe`` filename extension,
196 regardless of what platform you're using.
197
198 Compile flags for different development scenarios
199 =================================================
200
201 To optimize the performance of your .nexe module, you must use the correct set
202 of flags when you compile with nacl-gcc. If you're used to working with an IDE
203 rather than with a command-line compiler like gcc, you may not be familiar with
204 which flags you need to specify. The table below summarizes which flags to
205 specify based on different development scenarios.
206
207 ===================== ========================================================== =======
208 Development scenarios Flags for nacl-gcc
209 ===================== ========================================================== =======
210 debugging -g -O0
211 profile [-g] -O{2|3} -msse -mfpmath=sse -ffast-math -fomit-frame-p ointer
212 deploy -s -O{2|3} -msse -mfpmath=sse -ffast-math -fomit-frame-poi nter
213 ===================== ========================================================== =======
214
215 A few of these flags are described below:
216
217 -g
218 Produce debugging information.
219
220 -On
221 Optimize the executable for both performance and code size. A higher n
222 increases the level of optimization. Use -O0 when debugging, and -O2 or -O3
223 for profiling and deployment.
224
225 The main difference between -O2 and -O3 is whether the compiler performs
226 optimizations that involve a space-speed tradeoff. It could be the case that
227 these optimizations are not desirable due to .nexe download size; you should
228 make your own performance measurements to determine which level of
229 optimization is right for you. When looking at code size, note that what you
230 generally care about is not the size of the .nexe produced by nacl-gcc, but
231 the size of the compressed .nexe that you upload to the Chrome Web Store.
232 Optimizations that increase the size of a .nexe may not increase the size of
233 the compressed .nexe that much.
234
235 For additional information about optimizations, see the `gcc optimization
236 options <http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html>`_. Note
237 that the -Os option (optimize for size) is not currently supported.
238
239 -s
240 Strip the .nexe, i.e., remove all symbol table and relocation information
241 from the executable.
242
243 As an alternative to using the -s option, you can store a copy of the
244 non-stripped .nexe somewhere so that you can extract symbol information from
245 it if necessary, and use the nacl-strip tool in the SDK to strip symbol
246 information from the .nexe that you deploy.
247
248 .. Note::
249 :class: note
250
251 To see how the examples in the SDK are built, run make in any of the example
252 subdirectories (e.g., examples/hello_world). The make tool displays the full
253 command lines it runs for each step of the build process (compiling, linking,
254 and generating Native Client manifest files).
255
256 For additional information about compiler options, see `gcc command options
257 <http://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html>`_.
258
259 Using make
260 ==========
261
262 This document doesn't cover how to use ``make``, but if you want to use
263 ``make`` to build your Native Client module, you can base your Makefile on the
264 ones in the SDK examples.
265
266 The Makefiles for the SDK examples build most of the examples in multiple
267 configurations (using different C libraries, targeting different architectures,
268 and using different levels of optimization). With a few exceptions (tumbler,
269 debugging, and dlopen), running ``make`` in each example's directory does the
270 following:
271
272 * creates a subdirectory called ``newlib``;
273
274 * builds .nexes for the x86-32, x86-64, and ARM architectures using the
275 newlib library;
276 * generates a Native Client manifest (.nmf) file for the newlib version of
277 the example;
278
279 * creates a subdirectory called ``glibc``;
280
281 * builds .nexes for the x86-32 and x86-64 architectures using the glibc
282 library;
283 * generates a Native Client manifest (.nmf) file for the glibc version of the
284 example;
285
286 * creates a subdirectory called ``windows``, ``linux``, or ``mac`` (depending
287 on your development machine);
288
289 * builds a Pepper plugin (.dll for Windows, .so for Linux/Mac) using the
290 hosted toolchain on your development machine;
291 * generates a Native Client manifest (.nmf) file for the glibc version of the
292 example;
293
294 * creates a subdirectory called ``pnacl``;
295
296 * builds a .pexe (architecture-independent Native Client executable) using
297 the newlib library; and
298 * generates a Native Client manifest (.nmf) file for the pnacl version of the
299 example;
300
301 .. Note::
302 :class: note
303
304 * The examples are also built using different optimization levels, and the
305 executable and manifest files are actually located in subdirectories called
306 "Debug" and "Release".
307 * The glibc library is not yet available for the ARM and PNaCl toolchains.
308 * Chrome does not yet directly support .pexe files, but the PNaCl toolchain
309 contains a tool to translate .pexes into .nexes.
310
311 Your Makefile can be simpler since you will not likely want to build so many
312 different configurations of your module. The example Makefiles define
313 numerous variables near the top (e.g., ``GLIBC_CCFLAGS``) that make it easy
314 to customize the commands that are executed for your project and the options
315 for each command.
316
317 In addition to building .nexe files, the example Makefiles also generate
318 Native Client manifest (.nmf) files, which your application points to from
319 the ``src`` attribute of an ``<embed>`` tag in its HTML file. For information
320 about Native Client manifest files, see the :ref:`Technical Overview
321 <application_files>`. The SDK includes a script called ``create_nmf.py`` (in
322 the ``tools/`` directory) that you can use to generate .nmf files. Run
323 "``python create_nmf.py --help``" to see the script's command-line options,
324 and look at the Makefiles in the SDK examples to see how to use the script to
325 generate a manifest file for modules compiled with either toolchain.
326
327 For details on how to use make, see the `GNU 'make' Manual
328 <http://www.gnu.org/software/make/manual/make.html>`_.
329
330 Libraries and header files provided with the SDK
331 ================================================
332
333 The Native Client SDK includes modified versions of standard toolchain-support
334 libraries, such as iberty, nosys, pthread, and valgrind, plus the relevant
335 header files.
336
337 The libraries are located in the following directories:
338
339 * x86 toolchains: toolchain/<platform>_x86_<library>/x86_64-nacl/lib32 and
340 /lib64 (for the 32-bit and 64-bit target architectures, respectively)
341 * ARM toolchain: toolchain/<platform>_arm_<library>/arm-nacl/lib
342
343 For example, on Windows, the libraries for the x86-64 architecture in the
344 newlib toolchain are in toolchain/win_x86_newlib/x86_64-nacl/lib64.
345
346 The standard gcc libraries are also available, in
347 toolchain/<platform>_<architecture>_<library>/lib.
348
349 The header files are in:
350
351 * x86 toolchains: toolchain/<platform>_x86_<library>/x86_64-nacl/include
352 * ARM toolchain: toolchain/<platform>_arm_<library>/arm-nacl/include
353
354 The toolchains intentionally leave out some standard libraries and header
355 files; in particular, for sandboxing reasons, the SDK doesn't support some
356 POSIX-specified items. For example, ``open(2)`` isn't included, and
357 ``close(2)`` doesn't precisely match the POSIX version.
358
359 Many other libraries have been ported for use with Native Client; for more
360 information, see the `naclports <http://code.google.com/p/naclports/>`_
361 project. If you port an open-source library for your own use, we recommend
362 adding it to naclports.
363
364 Here are descriptions of some of the Native Client-specific libraries provided
365 in the SDK:
366
367 libppapi.a
368 Implements the Pepper (PPAPI) C interface (needed for all applications that
369 use Pepper).
370
371 libppapi_cpp.a
372 Implements the Pepper (PPAPI) C++ interface.
373
374 libpthread.a
375 Implements the Native Client pthread interface.
376
377 libsrpc.a
378 Implements the Native Client RPC layer, and is used to implement the Pepper C
379 layer.
380
381 libimc.a
382 Implements the intermodule communications layer (IMC), which is used to
383 implement SRPC, the Native Client RPC library.
384
385 libgio.a
386 Used to implement Native Client logging and some other features in
387 nacl_platform.
388
389 libplatform.a
390 Provides some platform abstractions, and is used to implement some other
391 Native Client libraries.
392
393 The top-level /lib directory contains two additional Native Client libraries of
394 interest:
395
396 libnacl_mounts.a
397 Provides a virtual file system that a module can "mount" in a given directory
398 tree. Once a module has mounted a file system, it can use standard C library
399 file operations: fopen, fread, fwrite, fseek, and fclose. For a list of the
400 types of file systems that can be mounted, see
401 include/nacl_mounts/nacl_mounts.h. For an example of how to use nacl_mounts,
402 see examples/hello_nacl_mounts.
403
404 libppapi_main.a
405 Provides a familiar C programming environment by letting a module have a
406 simple entry point called ppapi_main(), which is similar to the standard C
407 main() function, complete with argc and argv[] parameters. This library also
408 lets modules use standard C functions such as printf(), fopen(), and
409 fwrite(). For details see include/ppapi_main/ppapi_main.h. For an example of
410 how to use ppapi_main, see examples/hello_world_stdio.
411
412 .. Note::
413 :class: note
414
415 * Since the Native Client toolchains use their own library and header search
416 paths, the tools won't find third-party libraries you use in your
417 non-Native-Client development. If you want to use a specific third-party
418 library for Native Client development, look for it in `naclports
419 <http://code.google.com/p/naclports/>`_, or port the library yourself.
420 * The order in which you list libraries in your build commands is important,
421 since the linker searches and processes libraries in the order in which they
422 are specified. See the \*_LDFLAGS variables in the Makefiles of the SDK
423 examples for the order in which specific libraries should be listed.
424
425 Troubleshooting
426 ===============
427
428 Some common problems, and how to fix them:
429
430 "Undefined reference" error
431 An "undefined reference" error may indicate incorrect link order and/or
432 missing libraries. For example, if you leave out -lppapi when compiling the
433 hello_world example, you'll see a series of undefined reference errors.
434
435 One common type of "undefined reference" error is with respect to certain
436 system calls, e.g., "undefined reference to 'mkdir'". For security reasons,
437 Native Client does not support a number of system calls. Depending on how
438 your code uses such system calls, you have a few options:
439
440 #. Link with the -lnosys flag to provide empty/always-fail versions of
441 unsupported system calls. This will at least get you past the link stage.
442 #. Find and remove use of the unsupported system calls.
443 #. Create your own implementation of the unsupported system calls to do
444 something useful for your application.
445
446 If your code uses mkdir or other file system calls, you might find nacl-mounts
447 useful. Nacl-mounts essentially does option (3) for you: It lets your code
448 use POSIX-like file system calls, and implements the calls using various
449 technologies (e.g., App Engine or an in-memory filesystem).
450
451 Can't find libraries containing necessary symbols
452 Here is one way to find the appropriate library for a given symbol::
453
454 nm -o toolchain/<platform>_x86_<library>/x86_64-nacl/lib64/*.a | grep <MySym bolName>
OLDNEW
« no previous file with comments | « native_client_sdk/src/doc/Makefile ('k') | native_client_sdk/src/doc/devguide/devcycle/dynamic-loading.rst » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698