Chromium Anatomy & Code Reuse Series: Hello, World!

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

In this series of Chromium Anatomy & Code Reuse, 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值