Skip to content

Instantly share code, notes, and snippets.

@mohamed
Last active September 23, 2022 06:25
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 25 You must be signed in to fork a gist
  • Save mohamed/4fa7eb75807463d4dfa3 to your computer and use it in GitHub Desktop.
Save mohamed/4fa7eb75807463d4dfa3 to your computer and use it in GitHub Desktop.
Build Google gn build tool standalone
#!/usr/bin/env sh
set -euv
# Updated on April 2019 to reflect changes in GN
# See:
# https://github.com/ninja-build/ninja
# https://gn.googlesource.com/gn/
# We need a recent git
export PATH=/depot/git-2.8.3/bin:$PATH
# Directory where all the stuff will be built
rm -rf build_tools
mkdir build_tools
cd build_tools
# We install all to $HOME/bin
mkdir -p $HOME/bin
# Build ninja
git clone https://github.com/ninja-build/ninja.git
cd ninja
./configure.py --bootstrap
cp -f ninja $HOME/bin/
cd ..
# Setup tools for GN
export PATH=/depot/binutils-2.30/bin:/depot/gcc-6.2.0/bin:$HOME/bin:$PATH
export CC=gcc
export CXX=g++
export LDFLAGS=-lrt
# Build GN
git clone https://gn.googlesource.com/gn
cd gn
python build/gen.py
ninja -C out
# out/gn_unittests
cp -f out/gn $HOME/bin
@tojocky
Copy link

tojocky commented Aug 13, 2017

my latest working version: https://github.com/nodenative/nodenative/blob/master/build/gn-standalone.sh

To build the latest changes always is tricky and a often is broken. e.g. https://bugs.chromium.org/p/chromium/issues/detail?id=725439#c16

@paulreimer
Copy link

paulreimer commented Dec 17, 2017

FWIW I converted this to Dockerfile syntax in a build stage and it worked well for me:

RUN mkdir -p /opt/local/gn-standalone
WORKDIR /opt/local/gn-standalone

RUN git clone --depth 1 https://chromium.googlesource.com/chromium/src/base
RUN git clone --depth 1 https://chromium.googlesource.com/chromium/src/build
RUN git clone --depth 1 https://chromium.googlesource.com/chromium/src/build/config

RUN mkdir -p tools
RUN git clone --depth 1 https://chromium.googlesource.com/chromium/src/tools/gn tools/gn

RUN mkdir -p testing
RUN git clone --depth 1 https://chromium.googlesource.com/chromium/testing/gtest testing/gtest

RUN mkdir -p third_party/libevent
ADD https://chromium.googlesource.com/chromium/chromium/+archive/master/third_party/libevent.tar.gz third_party/libevent

WORKDIR /opt/local/gn-standalone/tools/gn
RUN ./bootstrap/bootstrap.py -s

@paulreimer
Copy link

paulreimer commented Dec 17, 2017

And if you don't like ADD and you already have curl then you could use this snippet instead:

RUN mkdir -p third_party/libevent
WORKDIR /opt/local/gn-standalone/third_party/libevent
RUN curl -L https://chromium.googlesource.com/chromium/chromium/+archive/master/third_party/libevent.tar.gz | tar xz

@uvguy
Copy link

uvguy commented Feb 11, 2018

damn, you could just easly download the whole chromium tar ball here , no need to mess with complicated setup like this. Just be sure to pick the lastest update . In linux with custom clang svn build at chromium_src/tools/gn/bootstrap.py remove -latomic ldflags and run it (bootstrap.py) with python2. Hopefully will be useful small tips.

@glaubitz
Copy link

Fails with:

# Build
cd tools/gn
./bootstrap/bootstrap.py -s
Building gn manually in a temporary directory for bootstrapping...
ninja: Entering directory `/tmp/tmpsvuDoi'
ninja: error: '/srv/glaubitz/tmp/gn-standalone/buildtools/third_party/libc++/trunk/src/algorithm.cpp', needed by '/srv/glaubitz/tmp/gn-standalone/buildtools/third_party/libc++/trunk/src/algorithm.o', missing and no known rule to make it
Command '['ninja', '-C', '/tmp/tmpsvuDoi', '-w', 'dupbuild=err', 'gn']' returned non-zero exit status 1

Debian unstable/sparc64.

@zopieux
Copy link

zopieux commented Apr 30, 2018

Can confirm @glaubitz issue. Solved it by adding two new dependencies (libc++, libc++abi). Please see this PKGBUILD for a successful build recipe.

@paulocoutinhox
Copy link

paulocoutinhox commented May 21, 2018

I got this error:


# Build
cd tools/gn
./bootstrap/bootstrap.py -s
Building gn manually in a temporary directory for bootstrapping...
ninja: Entering directory `/var/folders/mx/3gnl5sks49z37khmfv2l3qyr0000gn/T/tmpTMWf7W'
[11/373] CXX base/allocator/allocator_check.o
FAILED: base/allocator/allocator_check.o 
c++ -MMD -MF base/allocator/allocator_check.o.d  -I/var/folders/mx/3gnl5sks49z37khmfv2l3qyr0000gn/T/tmpTMWf7W/gen -I/Users/paulo/Developer/workspaces/cpp/mobile-pdfium/gn-standalone -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -O2 -g0 -D_FILE_OFFSET_BITS=64 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -pthread -pipe -fno-exceptions -std=c++14 -Wno-c++11-narrowing -c /Users/paulo/Developer/workspaces/cpp/mobile-pdfium/gn-standalone/base/allocator/allocator_check.cc -o base/allocator/allocator_check.o
In file included from /Users/paulo/Developer/workspaces/cpp/mobile-pdfium/gn-standalone/base/allocator/allocator_check.cc:19:
/Users/paulo/Developer/workspaces/cpp/mobile-pdfium/gn-standalone/base/allocator/allocator_interception_mac.h:11:10: fatal error: 'third_party/apple_apsl/malloc.h' file not found
#include "third_party/apple_apsl/malloc.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
[16/373] CXX base/allocator/allocator_extension.o
ninja: build stopped: subcommand failed.
Command '['ninja', '-C', '/var/folders/mx/3gnl5sks49z37khmfv2l3qyr0000gn/T/tmpTMWf7W', '-w', 'dupbuild=err', 'gn']' returned non-zero exit status 1

Mac OSX high sierra

@mxi1
Copy link

mxi1 commented Sep 6, 2018

@robertleeplummerjr
Copy link

Just download it from: https://gn.googlesource.com/gn/

@huenchao
Copy link

ninja: Entering directory `out'
[1/37] LINK gn
FAILED: gn
clang++ -L/usr/local/opt/libffi/lib -O3 -fdata-sections -ffunction-sections -Wl,-dead_strip -mmacosx-version-min=10.9 -pthread -o gn src/gn/gn_main.o base.a gn_lib.a
ld: warning: ignoring file base.a, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )
ld: warning: ignoring file gn_lib.a, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )
Undefined symbols for architecture x86_64:
"base::CommandLine::ForCurrentProcess()", referenced from:
_main in gn_main.o
"base::CommandLine::Init(int, char const* const*)", referenced from:
_main in gn_main.o
"base::CommandLine::GetArgs() const", referenced from:
_main in gn_main.o
"commands::kHelp", referenced from:
_main in gn_main.o
"MsgLoop::MsgLoop()", referenced from:
_main in gn_main.o
"commands::GetCommands()", referenced from:
_main in gn_main.o
"base::CommandLine::HasSwitch(char const*) const", referenced from:
_main in gn_main.o
"Location::Location()", referenced from:
_main in gn_main.o
"PrintShortHelp(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&)", referenced from:
_main in gn_main.o
"commands::CommandSwitches::Init(base::CommandLine const&)", referenced from:
_main in gn_main.o
"OutputString(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, TextDecoration, HtmlEscaping)", referenced from:
_main in gn_main.o
"Err::PrintToStdout() const", referenced from:
_main in gn_main.o
"switches::kVersion", referenced from:
_main in gn_main.o
"Err::Err(Location const&, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&)", referenced from:
_main in gn_main.o
"MsgLoop::~MsgLoop()", referenced from:
_main in gn_main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[14/37] CXX src/gn/runtime_deps_unittest.o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment