C++ Tutorial: Getting Started (Part 2)
Overview
This tutorial shows how to convert the finished PNaCl web application from Part 1 to use the Native Client SDK build system and common JavaScript files. It also demonstrates some techniques to make your web application Content Security Policy (CSP)-compliant <http://developer.chrome.com/apps/contentSecurityPolicy.html>, which is necessary for Chrome Apps.
Using the Native Client SDK build system makes it easy to build with all of the SDK toolchains, and switch between the Debug and Release configurations. It also simplifies the makefiles for your project, as we’ll see in the next section. Finally, it adds some useful commands for running and debugging your application.
The finished code for this example can be found in the
pepper_$(VERSION)/getting_started/part2
directory in the Native Client SDK
download.
Using the Native Client SDK build system
This section describes how to use the SDK build system. To do so, we’ll make changes in the makefile. Because the makefile in part1 and part2 are so different, it is easier to start from scratch. Here is the contents of the new makefile. The following sections will describe it in more detail.
Simplifying the Makefile
The makefile from part1 only supports one toolchain (PNaCl) and one configuration (Release). It also only supports one source file. It’s relatively simple, but if we want to add support for multiple toolchains, configurations, source files, or build steps, it would grow increasingly complex. The SDK build system uses a set of variables and macros to make this possible, without significantly increasing the complexity of the makefile.
Here is the new makefile, supporting three toolchains (PNaCl, Newlib NaCl, Glibc NaCl) and two configurations (Debug, Release).
VALID_TOOLCHAINS := pnacl newlib glibc NACL_SDK_ROOT ?= $(abspath $(CURDIR)/../..) include $(NACL_SDK_ROOT)/tools/common.mk TARGET = part2 LIBS = ppapi_cpp ppapi CFLAGS = -Wall SOURCES = hello_tutorial.cc # Build rules generated by macros from common.mk: $(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS)))) ifeq ($(CONFIG),Release) $(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS))) $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) else $(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS))) endif $(eval $(call NMF_RULE,$(TARGET),))
Choosing valid toolchains, and including common.mk
The makefile begins by specifying the toolchains that are valid for this project. The Native Client SDK build system supports multi-toolchain projects for its examples and libraries, but generally you will choose one toolchain when you begin your project and never change it. Please see the Toolchains section of the Native Client overview for more information.
For this example, we support the pnacl
, newlib
and glibc
toolchains.
VALID_TOOLCHAINS := pnacl newlib glibc
Next, as a convenience, we specify where to find NACL_SDK_ROOT
. Because
this example is located in pepper_$(VERSION)/getting_started/part2
, the
root of the SDK is two directories up.
NACL_SDK_ROOT ?= $(abspath $(CURDIR)/../..)
Next, we include the file tools/common.mk
. This file provides the
functionality for the Native Client SDK build system, including new build rules
to compile and link a project, which we’ll use below.
include $(NACL_SDK_ROOT)/tools/common.mk
Configuring your project
After including tools/common.mk
, we configure the project by specifying its
name, the sources and libraries it uses:
TARGET = part2 LIBS = ppapi_cpp ppapi CFLAGS = -Wall SOURCES = hello_tutorial.cc
These variable names are not required and not used by the SDK build system; they are only used in the rules described below. By convention, all SDK makefiles use the following variables:
- TARGET
- The name of the project to build. This variable determines the name of the
library or executable that will be generated. In the above example, we call
the target
part2
, which will generate an executable calledpart2.pexe
for PNaCl. For NaCl toolchains, the executable’s file name will be given a suffix for its architecture. For example, the ARM executable is calledpart2_arm.nexe
. - LIBS
- A list of libraries that this executable needs to link against. The library
search path is already set up to only look in the directory for the current
toolchain and architecture. In this example, we link against
ppapi_cpp
andppapi
.ppapi_cpp
is needed to use the Pepper C++ interface.ppapi
is needed for communicating with the browser. - CFLAGS
- A list of extra flags to pass to the compiler. In this example, we pass
-Wall
, which turns on all warnings. - LDFLAGS
- A list of additional flags to pass to the linker. This example does not need any special linker flags, so this variable is omitted.
- SOURCES
- A list of C or C++ sources to compile, separated by spaces. If you have a
long list of sources, it may be easier to read if you put each file on its
own line, and use
\
as a line-continuation character. Here’s an example:
SOURCES = foo.cc \ bar.cc \ baz.cc \ quux.cc
Build macros
For many projects, the following build macros do not need to be changed; they will use the variables we’ve defined above.
$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS)))) ifeq ($(CONFIG),Release) $(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS))) $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) else $(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS))) endif $(eval $(call NMF_RULE,$(TARGET),))
The first line defines rules to compile each source in SOURCES
, using the
flags in CFLAGS
:
$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))
The next six lines define rules to link the object files into one or more
executables. When TOOLCHAIN
is pnacl
, there is only one executable
generated: in the example above, part2.pexe
. When using a NaCl toolchain,
there will be three executables generated, one for each architecture: in the
example above, part2_arm.nexe
, part2_x86_32.nexe
and
part2_x86_64.nexe
.
When CONFIG
is Release
, each executable is also stripped to remove
debug information and reduce the file size:
ifeq ($(CONFIG),Release) $(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS))) $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) else $(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS))) endif
Finally, the NMF rule generates a NaCl manifest file (.nmf
) that references
each executable generated in the previous step:
$(eval $(call NMF_RULE,$(TARGET),))
Making index.html work for Chrome Apps
This section describes the changes necessary to make the HTML and JavaScript in part1 CSP-compliant. This is required if you want to build a Chrome App, but is not necessary if you want to use PNaCl on the open web.
CSP rules
Chrome Apps CSP restricts you from doing the following:
- You can’t use inline scripting in your Chrome App pages. The restriction
bans both
<script>
blocks and event handlers (<button onclick="...">
). - You can’t reference any external resources in any of your app files (except for video and audio resources). You can’t embed external resources in an iframe.
- You can’t use string-to-JavaScript methods like
eval()
andnew Function()
.
Making index.html CSP-compliant
To make our application CSP-compliant, we have to remove inline scripting. As
described above, we can’t use inline <script>
blocks or event handlers. This
is easy to do—we’ll just reference some new files from our script tag, and
remove all of our inlined scripts:
<head> ... <script type="text/javascript" src="common.js"></script> <script type="text/javascript" src="example.js"></script> </head>
common.js
has shared code used by all SDK examples, and is described
later in this document. example.js
is a script that has code specific to
this example.
We also need to remove the inline event handler on the body tag:
<body onload="pageDidLoad()"> ...
This logic is now handled by common.js
.
Making index.html support different toolchains and configurations
Finally, there are a few changes to index.html
that are not necessary for
CSP-compliance, but help make the SDK examples more generic.
First, we add some data attributes
to the body element to specify the name, supported toolchains, supported
configurations, and path to the .nmf
file:
<body data-name="part2" data-tools="newlib glibc pnacl" data-configs="Debug Release" data-path="{tc}/{config}"> ...
common.js
will read these data attributes to allow you to load the same
example with different toolchains by changing the URL’s query string. For example, you can load the
glibc Debug version of this example by navigating to
index.html?tc=glibc&config=Debug
.
Next, we remove the embed
element that is described in HTML. This will be
automatically added for us by common.js
, based on the current
toolchain/configuration combination:
<!-- Just as in part1, the <embed> element will be wrapped inside the <div> element with the id "listener". In part1, the embed was specified in HTML, here the common.js module creates a new <embed> element and adds it to the <div> for us. --> <div id="listener"></div>
Example-specific behavior with example.js
As described in the previous section, common.js
will call certain functions
during the module loading process. This example only needs to respond to two:
moduleDidLoad
and handleMessage
.
// This function is called by common.js when the NaCl module is // loaded. function moduleDidLoad() { // Once we load, hide the plugin. In this example, we don't display anything // in the plugin, so it is fine to hide it. common.hideModule(); // After the NaCl module has loaded, common.naclModule is a reference to the // NaCl module's <embed> element. // // postMessage sends a message to it. common.naclModule.postMessage('hello'); } // This function is called by common.js when a message is received from the // NaCl module. function handleMessage(message) { var logEl = document.getElementById('log'); logEl.textContent += message.data; }