Saturday, August 16, 2014

Getting basic gclient checkout setup.

In order to get our gclient workflow/build system going we obviously need to get gclient. So how do we do this?

Googling for gclient takes us to http://code.google.com/p/gclient/, which says that gclient is now a part of depot_tools with a link to depot_tools (http://dev.chromium.org/developers/how-tos/depottools) and an svn checkout command to get the latest set of tools:

$ svn checkout http://src.chromium.org/svn/trunk/tools/depot_tools

So, let's do just that. We need to check out the tools to somewhere accessible by the user, since this will be a set of tools we will consistently use for all the projects we might create. I put it under my home directory. You might have a better organizational structure; whatever works.

As an aside, it's somewhat discouraging to see depot_tools website talks primarily about Chromium source code. However, I think we can make it work with any project.

Now that I have ~/depot_tools checked out, I can see that it has quite a few tools, including gclient.

Next, we need to add this to our path, so that we can execute the commands without specifying the full path. That's easy enough:

$ export PATH=~/depot_tools:$PATH

I put that in my .bashrc so that it's always set whenever I start a terminal. Note that on my macbook, I also had to edit .bash_profile and put

source ~/.bashrc

in it, since that's what is actually run when I enter a terminal, it enters a login shell.

Moving on. I can now run gclient, so let's see what that gives me:

$ gclient
Usage: gclient.py  [options]

Meta checkout manager supporting both Subversion and GIT.

Commands are:
  cleanup  cleans up all working copies
  config   creates a .gclient file in the current directory
  diff     displays local diff for every dependencies
  fetch    fetches upstream commits for all modules
  grep     greps through git repos managed by gclient
  help     prints list of commands or help for a specific command
  hookinfo outputs the hooks that would be run by `gclient runhooks`
  pack     generates a patch which can be applied at the root of the tree
  recurse  operates [command args ...] on all the dependencies
  revert   reverts all modifications in every dependencies
  revinfo  outputs revision info mapping for the client and its dependencies
  runhooks runs hooks for files that have been modified in the local working copy
  status   shows modification status for every dependencies
  sync     checkout/update all modules

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -j JOBS, --jobs=JOBS  Specify how many SCM commands can run in parallel;
                        defaults to 8 on this machine
  -v, --verbose         Produces additional output for diagnostics. Can be
                        used up to three times for more logging info.
  --gclientfile=CONFIG_FILENAME
                        Specify an alternate .gclient file
  --spec=SPEC           create a gclient file containing the provided string.
                        Due to Cygwin/Python brokenness, it can't contain any
                        newlines.
  --no-nag-max          Ignored for backwards compatibility.

Ok. I think the first thing I need to do is to run config, since I know I need a .gclient file. I tried to follow instructions on http://dev.chromium.org/developers/how-tos/depottools but it seems to have a few broken links when it comes to .gclient examples. So, let's just try putting a repository on the line and run

$ gclient config ssh://example.com/repos/my_project.git

Note that this doesn't seem to do anything more than creating a .gclient file. In particular it doesn't try accessing that URL. I know this, since my first attempt had a typo and pointed to a non-existent repo.

$ cat .gclient
solutions = [
  { "name"        : "my_project",
    "url"         : "ssh://example.com/repos/my_project.git",
    "deps_file"   : "DEPS",
    "managed"     : True,
    "custom_deps" : {
    },
    "safesync_url": "",
  },
]
cache_dir = None

Ok, let's see if we can make sense of this. Solutions seems to refer to what I would call a project. It's an array of dictionaries (or something like that). Basically, each solution would refer to a full checkout of some particular project. Url is pretty self explanatory: it's the place where I can checkout my project. Deps refers to dependencies of this project. More precisely, it refers to a file in the checkout that lists dependencies. Managed, custom_deps, safesync_url, and cache_dir probably have their uses as well, but I'll leave that for a later post. Note that gclient config also has a --git-deps flag with an explanation that it will generate a .DEPS.git instead of DEPS as the deps file. Honestly, I'm not sure what the difference is, but since I'm going to be using git, I want to rerun gclient config with --git-deps:

$ gclient config --git-deps ssh://example.com/repos/my_project.git
$ cat .gclient
solutions = [
  { "name"        : "my_project",
    "url"         : "ssh://example.com/repos/my_project.git",
    "deps_file"   : ".DEPS.git",
    "managed"     : True,
    "custom_deps" : {
    },
    "safesync_url": "",
  },
]
cache_dir = None

Hmm, so that just changed DEPS to .DEPS.git. I don't know what I expected. Let's just work with .DEPS.git. One other thing of note is that rerunning config wiped my previous .gclient file and created a new one. This is good to know if I want to add more projects in the future. I would have to edit the .gclient file manually, instead of relying on gclient config. I didn't see any flag jump off the page that would enable me to append to a .gclient file instead of overwriting it.

Alright. It's the moment of truth. Let's see if this works as expected.

$ gclient sync
Syncing projects: 100% (1/1), done.
$ ls -d my_project
my_project

Success! Note that my_project doesn't have .DEPS.git file, but gclient didn't mind. Next time, I'm going to try and add some dependencies.

Was this easier than using git clone? No, of course not. However, I have a sneaking suspicion that figuring out how to create dependencies in git, automatically running scripts upon checkout, and other things might be easier to do with gclient. The rule of thumb I like to follow is that if it's hard to begin using something, then eventually hard tasks will be easier to do using it. That's one of the reasons I use VIM. :)

- vmpstr

No comments:

Post a Comment