• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright 2018 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# This script does two checks on the state of the copied chromium directories:
7#  1. Ensure all files present are in their appropriate BUILD.gn.
8#  2. Ensure all .h files with a corresponding .cc file in chromium have their
9#     .cc copied here.
10#
11# This should be run with your working directory at the root of the chromium
12# clone.
13# Usage: clone_helper.sh <ninja build dir> <chromium src directory>
14#                        [run existence check]
15
16# Both of the loops below will insert any files they want to add to a BUILD.gn
17# at the end of the first 'sources =' block in the file.  It will insert it with
18# only two spaces instead of four, so you can find files inserted by the script
19# and adjust them as necessary.
20function add_source_file() {
21  sed ":a;/sources = \[/bb;n;ba; :b;/^  \]/bc;n;bb; :c; s#^  \]#  \"$1\",\n  ]#; :d;n;bd" -i "$2"
22}
23
24ninja_dir=$1
25chromium_dir=$2
26
27# The following check can be skipped by not passing a path to gn to the script,
28# since this operation is slow and can be considered more of a "clean up" than
29# really iterative.
30if [ $# -eq 3 ]; then
31  gn_path=$3
32  # Ensure all .h,.cc, and .c files under the directory xyz are in xyz/BUILD.gn.
33  # This helps ensure we don't have extra files laying around in directories
34  # that aren't tracked in the build files.  Currently, base/ is an exception to
35  # this test because it is a submodule.
36  for d in build crypto net testing url; do
37    diff -u \
38      <(find $d -type f \
39        -name '*.h' -o -name '*.cc' -o -name '*.c' | sort | \
40        sed '/net\/third_party\/quic/d') \
41      <($gn_path desc $1 $d:\* sources | \
42         sed '/^\s*\/\//!d;s/^\s*\/\/third_party\/chromium_quic\/src\///' | \
43         sort)
44  done
45  exit
46fi
47
48# This loops tries to catch the simplest class of build errors: missing includes
49# relative to the src directory and .cc files named identically to .h files.
50# This will not catch things like:
51#  - Third-party dependencies which include things relative to their own source
52#    directory.
53#  - Platform-specific implementation files (e.g. xyz.h and xyz_posix.cc).
54while :; do
55  # Ensure all .h files with a matching .cc in chromium are copied here and
56  # placed in xyz/BUILD.gn.  This helps eliminate some obvious linking errors.
57  for f in $(find crypto net testing url -name '*.h'); do
58    cc_file=$(echo $f | cut -f1 -d.).cc
59    [ ! -e $cc_file ] || continue
60    [ -e $chromium_dir/$cc_file ] || continue
61    mkdir -p $(dirname $cc_file)
62    d=$(echo $f | cut -f1 -d/)
63    cp $chromium_dir/$cc_file $(dirname $cc_file)
64    if [ -e $cc_file ]; then
65      echo "$cc_file >> $d/BUILD.gn"
66      rel_file=$(echo $cc_file | cut -f2- -d/)
67      add_source_file "$rel_file" $d/BUILD.gn
68    fi
69  done
70
71  # Try to build what we have so far and fix any obvious include errors.  If we
72  # were able to add an include to fix the error, we loop again to potentially
73  # add a new .cc file and try again.
74  output=$(ninja -C $ninja_dir)
75  if echo "$output" | grep -q "#include \""; then
76    f=$(echo "$output" | grep -m1 "#include" |\
77        sed 's/^.*#include "\([^"]\+\)".*$/\1/')
78    mkdir -p $(dirname $f)
79    echo $f
80    if cp $chromium_dir/$f $(dirname $f) || \
81        cp $chromium_dir/out/Default/gen/$f $(dirname $f); then
82      b=$(echo $f | cut -f1 -d/)/BUILD.gn
83      echo "$f >> $b"
84      add_source_file "$(echo $f | cut -f2- -d/)" $b
85    else
86      echo -e "$output"
87      break
88    fi
89  else
90    echo -e "$output"
91    break
92  fi
93done
94