Chrome Anatomy & Code Reuse Series: Hello, World!

[First written by Jason Jee, please keep the mark if forwarding.]

In this series of Chromium Anatomy I will not only study the code but also try to reuse its components in our own program! We can reuse its message loop, multi-thread/process model, IPC, sandboxing and so on. But before we’re able to do these, let’s find out how to build our own program with Chromium’s build system.

I’ll try to add some files in the code tree of Chromium. Our goal is to build an executable other than Chrome or its testing tools. After we’re done, ‘make’ in ‘src’ directory and we’ll get very simple program saying ‘Hello, World!’ This program has no dependency on Chromium’s code, but it is only built by Chromium’s build system.

First, let’s make a new directory in ‘src’ called ‘helloworld’, and then add a new file ‘helloworld.gyp’. Here comes a question. What is a gyp file? In the Chromium project, gyp is used to describe how to generate makefile of a module, a library or a tool of Chrome. You can specify the makefile target name of a component, its sources, include directories, dependencies, compiling flags and even you can specify conditions in gyp file. Now we’d like to have our own gyp file. Lines beginning with ‘#’ are comments. Here I keep some of them. You can copy them from any of the gyp files in Chromium. But pay attention to the license.

# Copyright…blabla…
# License…blabla…
{
  ‘targets’: [
    {
      'target_name': 'helloworld',
      'type': 'executable',
      'sources': [
        'helloworld.cc',
      ],
    }
  ],
}
# Local Variables:
# tab-width:2
# indent-tabs-mod:nil
# End:
# vim: set expandtab tabstop=2 shiftwidth=2:

List 1 – content of src/HelloWorld/HelloWorld.gyp

Now let’s focus on the JSON formatted lines. We have a target ‘helloworld’ with its type ‘executable’, which means that after we ‘make helloworld’, an executable will be generated. Its only source file is ‘helloworld.cc’. What if now you ‘make helloworld’ in ‘src’ directory? Errors happen. We don’t have a target named ‘helloworld’. This is what we’ll do next.

Take a look at ‘src/build/all.gyp’. You’ll find that it is the root gyp which we have no other gyp files dependent on. Add our ‘helloworld.gyp’ in the dependency list of target ‘All’. This will insert our gyp as a node of the whole gyp tree of Chromium. Now build tools of Chromium is able to generate makefiles with our target ‘helloworld’ by executing the script src/build/gyp_chromium.

Oh, I forgot to type something in the source file ‘helloworld.cc’. So let’s write some code in it (Kicking keyboard…). Now, go back to the directory ‘src’ and execute ‘make helloworld’, and find and execute the binary in the ‘src/out/…’ directory! See what?

Because we’ll have many samples including this one in this series, I’m going to make a new gyp node called ‘samples.gyp’ in a new directory ‘src/samples/’. The content of this new gyp file is as below.

# head
{
  ‘targets’: [
   
{
      '
target_name': 'samples_all',
      '
type': 'none',
      '
dependencies': [
        '
helloworld/helloworld.gyp:*',
     
],
   
},
 
],
}
# foot

List 2 – All samples’ parent gyp node

This time I’ll replace the ‘helloworld.gyp’ with this one in the dependency list of ‘src/build/all.gyp’. And from now on, I’ll append new gyp nodes into ‘sample.gyp’. Adding so many nodes into the very root gyp node will certainly cause it to be ugly. Also, if we want remove all samples from the code tree, all we need to do now is to delete ‘sample.gyp’ from dependencies of the root gyp. Again, rebuild the makefile and then execute ‘make helloworld’ or ‘make samples_all’, you will get your program we saw in the hello-world example.

OK. Once we find out where to write our own code, the door of Chromium opens.

Advertisement
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s