3 ######################################
4 C++ Tutorial: Getting Started (Part 2)
5 ######################################
15 This tutorial shows how to convert the finished PNaCl web application from
16 :doc:`Part 1 <tutorial-part1>` to use the Native Client SDK build system and
17 common JavaScript files. It also demonstrates some techniques to make your web
18 application `Content Security Policy (CSP)-compliant
19 </apps/contentSecurityPolicy>`_, which is necessary for `Chrome Apps
22 Using the Native Client SDK build system makes it easy to build with all of the
23 SDK toolchains, and switch between the Debug and Release configurations. It
24 also simplifies the makefiles for your project, as we'll see in the next
25 section. Finally, it adds some useful commands for :ref:`running
26 <running_the_sdk_examples>` and :ref:`debugging <debugging_the_sdk_examples>`
29 The finished code for this example can be found in the
30 ``pepper_$(VERSION)/getting_started/part2`` directory in the Native Client SDK
33 Using the Native Client SDK build system
34 ========================================
36 This section describes how to use the SDK build system. To do so, we'll make
37 changes in the makefile. Because the makefile in part1 and part2 are so
38 different, it is easier to start from scratch. Here is the contents of the new
39 makefile. The following sections will describe it in more detail.
41 Simplifying the Makefile
42 ------------------------
44 The makefile from part1 only supports one toolchain (PNaCl) and one
45 configuration (Release). It also only supports one source file. It's relatively
46 simple, but if we want to add support for multiple toolchains, configurations,
47 source files, or build steps, it would grow increasingly complex. The SDK build
48 system uses a set of variables and macros to make this possible, without
49 significantly increasing the complexity of the makefile.
51 Here is the new makefile, supporting three toolchains (PNaCl, Newlib NaCl,
52 Glibc NaCl) and two configurations (Debug, Release).
56 VALID_TOOLCHAINS := pnacl newlib glibc
58 NACL_SDK_ROOT ?= $(abspath $(CURDIR)/../..)
59 include $(NACL_SDK_ROOT)/tools/common.mk
62 LIBS = ppapi_cpp ppapi
65 SOURCES = hello_tutorial.cc
67 # Build rules generated by macros from common.mk:
69 $(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))
71 # The PNaCl workflow uses both an unstripped and finalized/stripped binary.
72 # On NaCl, only produce a stripped binary for Release configs (not Debug).
73 ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG))))
74 $(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
75 $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
77 $(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
80 $(eval $(call NMF_RULE,$(TARGET),))
82 Choosing valid toolchains, and including common.mk
83 --------------------------------------------------
85 The makefile begins by specifying the toolchains that are valid for this
86 project. The Native Client SDK build system supports multi-toolchain projects
87 for its examples and libraries, but generally you will choose one toolchain
88 when you begin your project and never change it. Please see the
89 :ref:`Toolchains section of the Native Client overview <toolchains>` for more
92 For this example, we support the ``pnacl``, ``newlib`` and ``glibc`` toolchains.
96 VALID_TOOLCHAINS := pnacl newlib glibc
98 Next, as a convenience, we specify where to find ``NACL_SDK_ROOT``. Because
99 this example is located in ``pepper_$(VERSION)/getting_started/part2``, the
100 root of the SDK is two directories up.
104 NACL_SDK_ROOT ?= $(abspath $(CURDIR)/../..)
109 In your own projects, you can use the absolute path to your installed SDK
110 here. You can also override this default by setting the ``NACL_SDK_ROOT``
111 environment variable. See :ref:`Step 5 of Part 1 of this tutorial
112 <tutorial_step_5>` for more details.
114 Next, we include the file ``tools/common.mk``. This file provides the
115 functionality for the Native Client SDK build system, including new build rules
116 to compile and link a project, which we'll use below.
120 include $(NACL_SDK_ROOT)/tools/common.mk
122 Configuring your project
123 ------------------------
125 After including ``tools/common.mk``, we configure the project by specifying its
126 name, the sources and libraries it uses:
131 LIBS = ppapi_cpp ppapi
134 SOURCES = hello_tutorial.cc
136 These variable names are not required and not used by the SDK build system;
137 they are only used in the rules described below. By convention, all SDK
138 makefiles use the following variables:
141 The name of the project to build. This variable determines the name of the
142 library or executable that will be generated. In the above example, we call
143 the target ``part2``, which will generate an executable called
144 ``part2.pexe`` for PNaCl. For NaCl toolchains, the executable's file name
145 will be given a suffix for its architecture. For example, the ARM executable
146 is called ``part2_arm.nexe``.
149 A list of libraries that this executable needs to link against. The library
150 search path is already set up to only look in the directory for the current
151 toolchain and architecture. In this example, we link against ``ppapi_cpp``
152 and ``ppapi``. ``ppapi_cpp`` is needed to use the `Pepper C++ interface
153 </native-client/pepper_stable/cpp/>`_. ``ppapi`` is needed for communicating
157 A list of extra flags to pass to the compiler. In this example, we pass
158 ``-Wall``, which turns on all warnings.
161 A list of additional flags to pass to the linker. This example does not need
162 any special linker flags, so this variable is omitted.
165 A list of C or C++ sources to compile, separated by spaces. If you have a
166 long list of sources, it may be easier to read if you put each file on its
167 own line, and use ``\`` as a line-continuation character. Here's an example:
179 For many projects, the following build macros do not need to be changed; they
180 will use the variables we've defined above.
184 $(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))
186 ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG))))
187 $(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
188 $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
190 $(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
193 $(eval $(call NMF_RULE,$(TARGET),))
195 The first line defines rules to compile each source in ``SOURCES``, using the
200 $(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))
202 The next six lines define rules to link the object files into one or more
203 executables. When ``TOOLCHAIN`` is ``pnacl``, there is only one executable
204 generated: in the example above, ``part2.pexe``. When using a NaCl toolchain,
205 there will be three executables generated, one for each architecture: in the
206 example above, ``part2_arm.nexe``, ``part2_x86_32.nexe`` and
207 ``part2_x86_64.nexe``.
209 When ``CONFIG`` is ``Release``, each executable is also stripped to remove
210 debug information and reduce the file size. Otherwise, when the ``TOOLCHAIN``
211 is ``pnacl``, the workflow involves creating an unstripped binary for debugging
212 and then finalizing it and stripping it for publishing.
216 ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG))))
217 $(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
218 $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
220 $(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
223 Finally, the NMF rule generates a NaCl manifest file (``.nmf``) that references
224 each executable generated in the previous step:
228 $(eval $(call NMF_RULE,$(TARGET),))
230 Making index.html work for Chrome Apps
231 ======================================
233 This section describes the changes necessary to make the HTML and JavaScript in
234 part1 CSP-compliant. This is required if you want to build a `Chrome App
235 </apps/about_apps>`_, but is not necessary if you want to use PNaCl on the open
241 `Chrome Apps CSP </apps/contentSecurityPolicy#what>`_ restricts you from doing
244 * You can’t use inline scripting in your Chrome App pages. The restriction
245 bans both ``<script>`` blocks and event handlers (``<button onclick="...">``).
246 * You can’t reference any external resources in any of your app files (except
247 for video and audio resources). You can’t embed external resources in an
249 * You can’t use string-to-JavaScript methods like ``eval()`` and ``new
252 Making index.html CSP-compliant
253 -------------------------------
255 To make our application CSP-compliant, we have to remove inline scripting. As
256 described above, we can't use inline ``<script>`` blocks or event handlers. This
257 is easy to do---we'll just reference some new files from our script tag, and
258 remove all of our inlined scripts:
264 <script type="text/javascript" src="common.js"></script>
265 <script type="text/javascript" src="example.js"></script>
268 ``common.js`` has shared code used by all SDK examples, and is described
269 later in this document. ``example.js`` is a script that has code specific to
272 We also need to remove the inline event handler on the body tag:
276 <body onload="pageDidLoad()">
279 This logic is now handled by ``common.js``.
281 Making index.html support different toolchains and configurations
282 -----------------------------------------------------------------
284 Finally, there are a few changes to ``index.html`` that are not necessary for
285 CSP-compliance, but help make the SDK examples more generic.
287 First, we add some `data attributes
288 <https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes>`_
289 to the body element to specify the name, supported toolchains, supported
290 configurations, and path to the ``.nmf`` file:
294 <body data-name="part2"
295 data-tools="newlib glibc pnacl"
296 data-configs="Debug Release"
297 data-path="{tc}/{config}">
300 ``common.js`` will read these data attributes to allow you to load the same
301 example with different toolchains by changing the URL's `query string
302 <http://en.wikipedia.org/wiki/Query_string>`_. For example, you can load the
303 glibc Debug version of this example by navigating to
304 ``index.html?tc=glibc&config=Debug``.
306 Next, we remove the ``embed`` element that is described in HTML. This will be
307 automatically added for us by ``common.js``, based on the current
308 toolchain/configuration combination:
313 Just as in part1, the <embed> element will be wrapped inside the <div>
314 element with the id "listener". In part1, the embed was specified in HTML,
315 here the common.js module creates a new <embed> element and adds it to the
318 <div id="listener"></div>
320 Sharing common code with common.js
321 ==================================
323 ``common.js`` contains JavaScript code that each example uses to create a
324 NaCl module, handle messages from that module and other common tasks like
325 displaying the module load status and logging messages. Explaining all of
326 ``common.js`` is outside the scope of this document, but please look at the
327 documentation in that file for more information.
329 Loading the page and creating the module
330 ----------------------------------------
332 Since we've added ``<script>`` tags for ``common.js`` and ``example.js`` to the
333 ``head`` element, they will be loaded and executed before the rest of the
334 document has been parsed. As a result, we have to wait for the page to finish
335 loading before we try to create the embed element and add it to the page.
337 We can do that by calling ``addEventListener`` and listening for the
338 ``DOMContentLoaded`` event:
342 // Listen for the DOM content to be loaded. This event is fired when parsing of
343 // the page's document has finished.
344 document.addEventListener('DOMContentLoaded', function() {
348 Inside this function, we parse the URL query string, and compare that to the
353 // From https://developer.mozilla.org/en-US/docs/DOM/window.location
355 if (window.location.search.length > 1) {
356 var pairs = window.location.search.substr(1).split('&');
357 for (var key_ix = 0; key_ix < pairs.length; key_ix++) {
358 var keyValue = pairs[key_ix].split('=');
359 searchVars[unescape(keyValue[0])] =
360 keyValue.length > 1 ? unescape(keyValue[1]) : '';
366 var toolchains = body.dataset.tools.split(' ');
367 var configs = body.dataset.configs.split(' ');
371 var tc = toolchains.indexOf(searchVars.tc) !== -1 ?
372 searchVars.tc : toolchains[0];
374 // If the config value is included in the search vars, use that.
375 // Otherwise default to Release if it is valid, or the first value if
376 // Release is not valid.
377 if (configs.indexOf(searchVars.config) !== -1)
378 var config = searchVars.config;
379 else if (configs.indexOf('Release') !== -1)
380 var config = 'Release';
382 var config = configs[0];
384 Then ``domContentLoaded`` is called, which performs some checks to see if the
385 browser supports Native Client, then creates the NaCl module.
389 function domContentLoaded(name, tool, path, width, height, attrs) {
390 updateStatus('Page loaded.');
391 if (!browserSupportsNaCl(tool)) {
393 'Browser does not support NaCl (' + tool + '), or NaCl is disabled');
394 } else if (common.naclModule == null) {
395 updateStatus('Creating embed: ' + tool);
397 // We use a non-zero sized embed to give Chrome space to place the bad
398 // plug-in graphic, if there is a problem.
399 width = typeof width !== 'undefined' ? width : 200;
400 height = typeof height !== 'undefined' ? height : 200;
401 attachDefaultListeners();
402 createNaClModule(name, tool, path, width, height, attrs);
404 // It's possible that the Native Client module onload event fired
405 // before the page's onload event. In this case, the status message
406 // will reflect 'SUCCESS', but won't be displayed. This call will
407 // display the current message.
408 updateStatus('Waiting.');
412 ``attachDefaultListeners`` is added before the creation of the module, to make
413 sure that no messages are lost. Note that ``window.attachListeners`` is also
414 called; this is the way that ``common.js`` allows each example to configure
415 itself differently. If an example defines the ``attachListeners`` function, it
416 will be called by ``common.js``.
420 function attachDefaultListeners() {
421 var listenerDiv = document.getElementById('listener');
422 listenerDiv.addEventListener('load', moduleDidLoad, true);
423 listenerDiv.addEventListener('message', handleMessage, true);
424 listenerDiv.addEventListener('crash', handleCrash, true);
425 if (typeof window.attachListeners !== 'undefined') {
426 window.attachListeners();
430 Finally, ``createNaClModule`` actually creates the ``embed``, and appends it as
431 a child of the element with id ``listener``:
435 function createNaClModule(name, tool, path, width, height, attrs) {
436 var moduleEl = document.createElement('embed');
437 moduleEl.setAttribute('name', 'nacl_module');
438 moduleEl.setAttribute('id', 'nacl_module');
439 moduleEl.setAttribute('width', width);
440 moduleEl.setAttribute('height', height);
441 moduleEl.setAttribute('path', path);
442 moduleEl.setAttribute('src', path + '/' + name + '.nmf');
446 var mimetype = mimeTypeForTool(tool);
447 moduleEl.setAttribute('type', mimetype);
449 var listenerDiv = document.getElementById('listener');
450 listenerDiv.appendChild(moduleEl);
454 When the module finishes loading, it will dispatch a ``load`` event, and the
455 event listener function that was registered above (``moduleDidLoad``) will be
456 called. Note that ``common.js`` allows each example to define a
457 ``window.moduleDidLoad`` function, that will be called here as well.
461 function moduleDidLoad() {
462 common.naclModule = document.getElementById('nacl_module');
463 updateStatus('RUNNING');
465 if (typeof window.moduleDidLoad !== 'undefined') {
466 window.moduleDidLoad();
470 Example-specific behavior with example.js
471 =========================================
473 As described in the previous section, ``common.js`` will call certain functions
474 during the module loading process. This example only needs to respond to two:
475 ``moduleDidLoad`` and ``handleMessage``.
479 // This function is called by common.js when the NaCl module is
481 function moduleDidLoad() {
482 // Once we load, hide the plugin. In this example, we don't display anything
483 // in the plugin, so it is fine to hide it.
486 // After the NaCl module has loaded, common.naclModule is a reference to the
487 // NaCl module's <embed> element.
489 // postMessage sends a message to it.
490 common.naclModule.postMessage('hello');
493 // This function is called by common.js when a message is received from the
495 function handleMessage(message) {
496 var logEl = document.getElementById('log');
497 logEl.textContent += message.data;