GN Build add subdirectory that has a makefile

446 Views Asked by At

I am trying to add a functionality to the chromium that requires the third party library tpm2-tss. This library has a makefile to build it.

I have added a folder tpm in the third_party directory and added a BUILD.gn file. Also the library is added as a subdirectory in this folder.

thirdparty
   ...other third-party folders
   tpm
      BUILD.gn
      tmp2-tss/
         Makefile
         ...

I tried adding the library by using

config("tpm_config"){
  include_dirs = ["tpm2-tss"]
}

in the BUILD.gn and then using configs += ["//thirdparty/tpm:tpm_config"] where I want to use the library. This of course does not work as the tpm2-tss library is not header-only. However, I can't find a way to tell the GN Build system to build/link this library using the Makefile it provides.

Is there a way to use the makefile inside GN or do I need to rewrite the Makefile into a BUILD.gn?

This is just for a proof of concept so I don't need a proper solution that would satisfy the usual standards of the chromium repository.

1

There are 1 best solutions below

1
On

GN doesn't know anything about other build systems, and requires you to build out actions (or template) for anything more than compiling / archiving / linking.

So, in your case, you might want to place a BUILD.gn file in the root directory of your third-party thing that looks like this:

action("tpm") {
  script = "make.py" # assumes you're using Python
  outputs = [ "${target_out_dir}/libtpm.a", ... ] # or whatever
  sources = [ ... ] # List tpm sources here
  args = [ ... ] # whatever make.py needs
}

If tpm doesn't change frequently, or if you always clean your build before you change it, you can skip the sources section; that's just there for GN to emit source-file dependencies on the target.

Or if this is something you do often, you could wrap it up in a template:

template("make") {
  action(target_name) {
    script = "//path/to/make.py"
    ...
  }
}

And then use it in your BUILD.gn file:

import("//path/to/make.gni")

make("tpm") {
  ...
}

Your make.py might look like this:

import subprocess
return subprocess.run(['make', '-j'], check=True).returncode