Passing a GYP FLAG that defines a macro visible to source files

Introduction

Javascript V8 build uses build/gyp project to generate build configuration files i.e., project files, makefiles etc. However, gyp is going to be deprecated soon and this will be replaced with GN, there is more info is on the wiki. Right now, we talk about gyp. GYPFlag is a way to pass various build options. This section on the wiki provides example of using GYPFLAG,

library=shared
disassembler=on
snapshot=off

We can pass options like this with make to change configuration of the build. Our target in this article is to modify it to add a GYPFLAG. When this GYPFLAG will be added a specified macro will be visible to source files.

Goal of this article

In previous section, we provided example of gyp variables which we can pass with make command,

$ make native library=shared

We want to add our custom variable which will enable a macro that is visible to source file.

$ make native my_component_enable_code=on

When my_component_enable_code=on is passed V8_MY_COMPONENT_ENABLE_CODE will be defined and visible to source files.

Changes on Makefile

In simply sentences, following allows us to use component_enable_code=on with make command,

ifeq ($(my_component_enable_code), on)
  GYPFLAGS += -Dv8_my_component_enable_code=1
endif

Changes on gypfiles/features.gypi

Under variables section we add following line to introduce a new variable,

'v8_my_component_enable_code%': 0,

Then, it looks like following,

{
  'variables': {
    'v8_enable_disassembler%': 0,
    'v8_my_component_enable_code%': 0,

so when it is referenced on later code block it is recognized.

If we want this only for debug build we add this to Debug section,

'DebugBaseCommon': {
  'abstract': 1,
  'variables': {
   'v8_enable_handle_zapping%': 1,
 },
 'conditions': [
   ['v8_enable_handle_zapping==1', {
  'defines': ['ENABLE_HANDLE_ZAPPING',],
   }],
   ['OS=="NEW_OS" and v8_my_component_enable_code==1', {
  'defines': ['V8_MY_COMPONENT_ENABLE_CODE',],
   }],
 ],

We add this to Release configuration which looks like,

'Release': {
  'variables': {
    'v8_enable_handle_zapping%': 1,
  },
  'conditions': [
    ['v8_enable_handle_zapping==1', {
      'defines': ['ENABLE_HANDLE_ZAPPING',],
    }],
   ['OS=="NEW_OS" and v8_my_component_enable_code==1', {
  'defines': ['V8_MY_COMPONENT_ENABLE_CODE',],
   }],

Adding those under DebugBaseCommon or Release work pretty well for us. However, there can be other ways to enable the definition depending on the requirement. For the changes we made so far, we can write code to conditionally enable when that argument is provided my_component_enable_code=on on command line with make,

#ifdef V8_MY_COMPONENT_ENABLE_CODE
  Handle<Context> CompEnv::Create() {
    // function body
    // cool code for cool component
  }
#endif

Building with this new option

Example, make command looks like following,

    make native my_component_enable_code=on -j 32

That’s pretty much it.

[updated August 24, 2016]

Leave a comment