gnfuchsia

How to build out-of-tree Fuchsia OS program


After installing and building Fuchsia OS, I can modify the string in the example hello world program from "Hello, World!\n" to "Hello, Fuchsia!\n". Then I build and execute the code which produces the expected string "Hello, Fuchsia!" using:

cd fuchsia
fx set bringup.x64 --with //examples/hello_world
fx build; fx qemu
hello_world_cpp

This is fine for understanding how to change part of the Fuchsia "distribution". How do I create my own program outside of the fuchsia tree? I assume one would do normally this when creating program for running on Fuchsia OS so that one can manage the source cleanly.


Solution

  • Answer

    The third_party directory is meant for modules that are managed outside of the Fuchsia tree. In the top-level .gitignore the directory is excluded (link):

    /third_party/*
    

    You can see that this folder is mostly empty in git (link). It is first populated during bootstrap (link), which internally uses jiri update to fetch repos specified in the integration manifest (e.g. for third_party).

    You would maintain your module in a separate git repo. For development, you would clone this repo into a sub-directory in third-party. Because of the .gitignore entry, it will not be tracked by the Fuchsia git.

    Example

    Files:

    third_party/hello_world/BUILD.gn
    third_party/hello_world/hello_world.cc
    

    BUILD.gn:

    import("//build/package.gni")
    
    group("hello_world") {
      deps = [ ":hello-world-cpp" ]
    }
    
    executable("bin") {
      output_name = "my_hello_world_cpp"
      sources = [ "hello_world.cc" ]
    }
    
    package("hello-world-cpp") {
      deps = [ ":bin" ]
      binaries = [
        {
          name = "my_hello_world_cpp"
        },
      ]
    }
    

    hello_world.cc:

    #include <iostream>
    
    int main(int argc, char** argv) {
      std::cout << "Hello, World (out-of-tree)!" << std::endl;
      return 0;
    }
    

    Build and run:

    $ fx set bringup.x64 --with //third_party/hello_world
    $ fx build
    $ fx qemu
    $ my_hello_world_cpp
    Hello, World (out-of-tree)!