[Instrumented libraries] Add guest-oslogin build target

guest-oslogin isn't a debian package, so this CL adds the ability
to build from git repos.  Additionally, it uses a handwritten
Makefile, so `configure` shouldn't be run.  This CL also adds
--no-configure for this purpose.

R=thestig

Change-Id: I709d926a465447e8f7ffbb340fd95382c7d715e8
Bug: 1496000
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4997397
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Thomas Anderson <thomasanderson@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1219180}
NOKEYCHECK=True
GitOrigin-RevId: b1dc450b32e2453ab3d25544a3f2258cf94300a9
diff --git a/focal/BUILD.gn b/focal/BUILD.gn
index e3001e0..604c2bb 100644
--- a/focal/BUILD.gn
+++ b/focal/BUILD.gn
@@ -13,6 +13,7 @@
     ":brltty",
     ":dee",
     ":freetype",
+    ":guest-oslogin",
     ":harfbuzz",
     ":libappindicator3-1",
     ":libasound2",
@@ -160,6 +161,18 @@
     if (defined(invoker.extra_configure_flags)) {
       args += [ "--extra-configure-flags=${invoker.extra_configure_flags}" ]
     }
+
+    if (defined(invoker.git_url)) {
+      args += [ "--git-url=${invoker.git_url}" ]
+    }
+
+    if (defined(invoker.git_revision)) {
+      args += [ "--git-revision=${invoker.git_revision}" ]
+    }
+
+    if (defined(invoker.no_configure) && invoker.no_configure) {
+      args += [ "--no-configure" ]
+    }
   }
 }
 
@@ -203,6 +216,15 @@
   extra_configure_flags = [ "--disable-static" ]
 }
 
+instrumented_library("guest-oslogin") {
+  git_url = "https://github.com/GoogleCloudPlatform/guest-oslogin.git"
+  git_revision = "f59b7f38c21b4794282ddf12fd4a6083cd99e1e4"
+  # guest-oslogin is built only with `make`.
+  no_configure = true
+  # Work around an issue where header files are passed to the linker.
+  patches = [ "patches/guest-oslogin.diff" ]
+}
+
 instrumented_library("harfbuzz") {
   package_cflags = [ "-Wno-c++11-narrowing" ]
   extra_configure_flags = [
diff --git a/focal/patches/guest-oslogin.diff b/focal/patches/guest-oslogin.diff
new file mode 100644
index 0000000..a3a99d3
--- /dev/null
+++ b/focal/patches/guest-oslogin.diff
@@ -0,0 +1,19 @@
+diff --git a/src/Makefile b/src/Makefile
+index a633c7c..39185fc 100644
+--- a/src/Makefile
++++ b/src/Makefile
+@@ -52,12 +52,12 @@ $(NSS_CACHE_OSLOGIN): nss/nss_cache_oslogin.o nss/compat/getpwent_r.o oslogin_ut
+ 
+ # PAM modules
+ 
+-$(PAM_LOGIN): pam/pam_oslogin_login.o oslogin_sshca.o oslogin_utils.o include/oslogin_sshca.h
++$(PAM_LOGIN): pam/pam_oslogin_login.o oslogin_sshca.o oslogin_utils.o
+ 	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -shared $^ -o $@ $(PAMLIBS)
+ 
+ # Utilities.
+ 
+-google_authorized_principals: authorized_principals/authorized_principals.o oslogin_utils.o oslogin_sshca.o include/oslogin_sshca.h
++google_authorized_principals: authorized_principals/authorized_principals.o oslogin_utils.o oslogin_sshca.o
+ 	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $^ -o $@ $(LDLIBS)
+ 
+ google_authorized_keys: authorized_keys/authorized_keys.o oslogin_utils.o
diff --git a/focal/scripts/download_build_install.py b/focal/scripts/download_build_install.py
index a5c4f22..db043fe 100755
--- a/focal/scripts/download_build_install.py
+++ b/focal/scripts/download_build_install.py
@@ -77,6 +77,11 @@
 
     self.init_build_env(eval(args.env))
 
+    self._git_url = args.git_url
+    self._git_revision = args.git_revision
+
+    self._no_configure = args.no_configure
+
     # Initialized later.
     self._source_dir = None
     self._source_archives = None
@@ -127,27 +132,32 @@
       shutil.rmtree(self._working_dir, ignore_errors=True)
       os.makedirs(self._working_dir)
 
-      # Download one source package at a time, otherwise, there will
-      # be connection errors in gnutls_handshake().
-      lock = open('apt-source-lock', 'w')
-      fcntl.flock(lock, fcntl.LOCK_EX)
-      self.shell_call('apt-get source %s' % self._package,
-                      cwd=self._working_dir)
-      fcntl.flock(lock, fcntl.LOCK_UN)
+      if self._git_url:
+        command = 'git clone %s' % self._git_url
+        self.shell_call(command, cwd=self._working_dir)
+      else:
+        # Download one source package at a time, otherwise, there will
+        # be connection errors in gnutls_handshake().
+        lock = open('apt-source-lock', 'w')
+        fcntl.flock(lock, fcntl.LOCK_EX)
+        command = 'apt-get source %s' % self._package
+        self.shell_call(command, cwd=self._working_dir)
+        fcntl.flock(lock, fcntl.LOCK_UN)
 
     (dirpath, dirnames, filenames) = next(os.walk(self._working_dir))
 
     if len(dirnames) != 1:
-      raise Exception(
-          '`apt-get source %s\' must create exactly one subdirectory.'
-              % self._package)
-    self._source_dir = os.path.join(dirpath, dirnames[0], '')
-
-    if len(filenames) == 0:
-      raise Exception('Can\'t find source archives after `apt-get source %s\'.'
-         % self._package)
-    self._source_archives = \
-        [os.path.join(dirpath, filename) for filename in filenames]
+      raise Exception( '`%s\' must create exactly one subdirectory.' % command)
+    self._source_component = dirnames[0]
+    self._source_dir = os.path.join(dirpath, self._source_component, '')
+    if self._git_url:
+      self.shell_call('git checkout %s' % self._git_revision,
+                      cwd=self._source_dir)
+    else:
+      if len(filenames) == 0:
+        raise Exception('Can\'t find source files after `%s\'.' % command)
+      self._source_archives = \
+          [os.path.join(dirpath, filename) for filename in filenames]
 
     return get_fresh_source
 
@@ -165,8 +175,12 @@
     """
     shutil.rmtree(self._source_archives_dir, ignore_errors=True)
     os.makedirs(self._source_archives_dir)
-    for filename in self._source_archives:
-      shutil.copy(filename, self._source_archives_dir)
+    if self._git_url:
+      dest = os.path.join(self._source_archives_dir, self._source_component)
+      shutil.copytree(self._source_dir, dest)
+    else:
+      for filename in self._source_archives:
+        shutil.copy(filename, self._source_archives_dir)
     for patch in self._patches:
       shutil.copy(patch, self._source_archives_dir)
 
@@ -246,9 +260,10 @@
     Builds the package with ./configure + make, installs it to a temporary
     location, then moves the relevant files to their permanent location.
     """
-    configure_cmd = './configure --libdir=/%s/ %s' % (
-        self._libdir, self._extra_configure_flags)
-    self.shell_call(configure_cmd, env=self._build_env, cwd=self._source_dir)
+    if not self._no_configure:
+      configure_cmd = './configure --libdir=/%s/ %s' % (
+          self._libdir, self._extra_configure_flags)
+      self.shell_call(configure_cmd, env=self._build_env, cwd=self._source_dir)
 
     # Some makefiles use BUILDROOT or INSTALL_ROOT instead of DESTDIR.
     args = ['DESTDIR', 'BUILDROOT', 'INSTALL_ROOT']
@@ -564,6 +579,9 @@
   # The LIBDIR argument to configure/make.
   parser.add_argument('--libdir', default='lib')
   parser.add_argument('--env', default='')
+  parser.add_argument('--git-url', default='')
+  parser.add_argument('--git-revision', default='')
+  parser.add_argument('--no-configure', action='store_true')
 
   # Ignore all empty arguments because in several cases gyp passes them to the
   # script, but ArgumentParser treats them as positional arguments instead of
diff --git a/focal/scripts/install-build-deps.sh b/focal/scripts/install-build-deps.sh
index 024cf9d..d05f508 100755
--- a/focal/scripts/install-build-deps.sh
+++ b/focal/scripts/install-build-deps.sh
@@ -32,6 +32,7 @@
 fontconfig \
 freetype \
 gdk-pixbuf \
+git \
 glib2.0 \
 gnome-common \
 gnome-keyring \