Adding, removing, and renaming files in iOS Chromium needs to follow a specific
procedure that will be unfamiliar to engineers coming from other iOS projects.
Conceptually, every file is recorded in four locations: the local filesystem,
git, the BUILD.gn
files, and the XCode projects. Of these, the XCode project
is wholly generated from the others.
[TOC]
Do not use XCode to manipulate files. The XCode project used for iOS
Chromium is generated; it's functionally a build artifact. The various
BUILD.gn
files in Chromium define structure of the XCode project file. Running
gclient runhooks
causes the project files to be regenerated.
Individual files can have their contents edited within XCode, and all of the regular testing and debugging activities can be done within XCode. It just can't be used to create files, rename files, delete files, or manipulate the project group structure in any way. To do these things, follow the procedures below.
To add any files (new headers, .mm
or .cc
implementation files, asset files
of any kind), the following general steps need to happen:
-
The file needs to exist in the correct directory of the file system for your Chromium checkout. New files need to be created, or assets need to be copied to the right location.
-
The new files need to be added to git.
-
The new files need to be added to a target in a
BUILD.gn
file (usually in the same directory as the newly added file). -
The XCode project needs to be regenerated.
For adding new header or implementation files, the following procedure is recommended:
-
Generate the new files using
tools/boilerplate.py
. This will generate the correct header guard macros and include the copyright boilerplate. -
Add the newly created files using
git add
. -
Edit the
BUILD.gn
file for the directory where the files were added. For each file, add it to thesources
list for the correctsource_set
in theBUILD.gn
file. Note thatgn format
(which is run as part ofgit cl format
) will take care of alphabetizing the lists along with other formatting, so there's no need to manually do these things. (BUILD.gn
files cand be edited directly in XCode, or in another editor such asvi
). -
Once all of the files have been created,
add
ed and allBUILD.gn
files have been updated, rungclient runhooks
to regenerate all XCode projects. -
If XCode is open, it may prompt to "Autocreate Schemes". If so, click on the highlighted "Automatically Create Schemes" button.
In the shell, this procedure would look like this:
// Step 1 -- generate new files.
$ tools/boilerplate.py ios/chrome/browser/some_feature/feature_class.h
$ tools/boilerplate.py ios/chrome/browser/some_feature/feature_class.mm
$ tools/boilerplate.py ios/chrome/browser/some_feature/feature_class_unittest.mm
// Step 2 -- add the new files.
$ git add ios/chrome/browser/some_feature/feature_class*
// Step 3 -- edit the BUILD.gn file in the editor of your choice
$ vi ios/chrome/browser/some_feature/BUILD.gn
// Step 4 -- regenerate the XCode Projects
$ gclient runhooks
To add asset files, follow this procedure:
-
Copy the asset files to the correct directory, with the correct names (including
@2x
and@3x
suffixes) Note that images are stored in.imageset
directories, conventionally insideresources
directories for a given UI feature. New directories, if needed, are created in the usual way (mkdir
). Note that there is no equivalent ofboilerplate.py
for images or other asset files. -
Create or copy
Contents.json
files for each new.imageset
directory, with the appropriate contents. -
Add all image and
Contents.json
files usinggit add
. -
Edit the
BUILD.gn
file in the containingresources
directory, addingimageset
entries for each added.imageset
directory, and then grouping all assets into a new or existinggroup()
declaration with apublic_deps
list containing all of theimageset
targets. -
Regenerate the XCode project with
gclient runhooks
.
To add Markdown documentation files, the procedure is much simpler. These files
are automatically added to the XCode project without BUILD.gn
entries, and
they have no required boilerplate. So adding new docs is as simple as:
-
Create a new
.md
file in the appropriate directory (docs/ios
, for example). -
git add
the file.
The newly added file will be visible in XCode after the next gclient runhooks
.
Renaming a file involves updating the filename in all of the places where it
exists: the file names in the filesystem, the file names in git, header guards
in files, import declarations in files, listings in BUILD.gn files, and
internally in the XCode project. As with adding a file, different tools are used
for each of these. Unlike creating a file, which starts with actually adding a
file to the filesystem, a rename starts with updating git (via git mv
), then
using the mass-rename
tool to update file contents.
tools/git/mass-rename.py
works by looking at uncommitted file moves in git,
and then updating all includes, header guards, and BUILD.gn entries to use the
new name. It doesn't update some other files, such as Contents.json
files for
image assets. It also doesn't change any symbols in code, so class and variable
names won't be changed.
For many file moves, it will be simpler to use another tool,
tools/git/move_source_file.py
, which combines git mv
and mass-rename
in a
single action. For example, renaming feature_class
to renamed_class
would be
done like this:
$ tools/git/move_source_file.py ios/chrome/browser/some_feature/feature_class.h \
ios/chrome/browser/some_feature/renamed_class.h
$ tools/git/move_source_file.py ios/chrome/browser/some_feature/feature_class.mm \
ios/chrome/browser/some_feature/renamed_class.mm
The step-by-step procedure for a rename is:
-
If there are other uncommitted changes before the move, it's usually cleanest to commit before starting the move.
-
move_source_file
each file that needs to be renamed. This renames the file in both the file system and in git, and in most places where it's used in code. -
Run
gclient runhooks
to update the XCode project. Check that all of the needed name changes have been made (for example, by building all targets). Make any other needed fixes. -
If any classes or other symbols need to be renamed (remember that the name of the primary interface in each file must match the file name), make those changes. Find-and-replace tools like
tools/git/mffr.py
or XCode's Find/Replace can help here, but there are no compiler-aware tools that can do a "smart" rename. -
Commit all changes (
git commit -a -m <your comment>
).
A move—where a file is moved to a different directory—is in most respects
performed using the same steps as a rename. However, while mass-rename.py
(and
thus move_source_file.py
) will update existing file names in BUILD.gn
files,
it won't move entries from one BUILD.gn
file to another. To move files to a
different directory, the preceding procedure is used, but between steps 2 and 3
(after moving the files, but before regenerating the XCode project), the old
filenames will need to be removed from the BUILD.gn
files in the old
directories and added to the BUILD.gn
files in the new directories.
Also note that while move_source_file
must be used separately for each file
being renamed within a directory, it (just like git mv
) can move multiple
files without renaming to a new directory in a single command:
$ tools/git/mass-rename.py ios/chrome/browser/some_feature/feature_class.* \
ios/chrome/browser/some_feature/feature_class_unittest.mm \
ios/chrome/browser/other_feature/
Deleting files follows the same patterns as adding and moving files. As with a file move, it's best to begin with deleting the files from git.
Typically, before actually removing a file, first all usage of the interface(s)
in the file(s) will be removed, and the file will no longer be #imported
anywhere.
Step-by step:
-
git rm
the files you want to remove. This will also remove the files from the filesystem. -
Manually remove the
BUILD.gn
entries for the files. -
Regenerate the XCode project (with
gclient runhooks
) to remove the files from XCode.
It's easy to miss some uses of a file that was renamed or deleted, and fixing compilation errors discovered in the commit queue means another commit-upload-dry run cycle (at least). To minimize this, after any change that adds, renames, moves, or deletes files, be sure to take the following steps:
-
git cl format
to update the formatting of all files. -
gn check
to make sure that any new or moved files follow the dependency rules (for example:gn check -C out/Debug-iphonesimulator/
). -
Build all targets, to make sure that everything has been added, changed, or removed correctly. This can be done by selecting the "All" target in XCode and building (
⌘-B
), or from the command line (for example,autoninja -C out/Debug-iphonesimulator/
).
Changes that involve adding or deleting more than a few files, and most renames of any size, should be in a single CL with no other changes, for ease of reviewing and (if necessary) reverting or cherry-picking.
If files are accidentally added, renamed, or moved through XCode, other settings in the XCode project may be changed that will introduce strange local build failures. In this case, take the following steps to recover.
-
Quit XCode.
-
Delete all generated XCode projects and associated files:
rm -rf out/build
. -
Regenerate all XCode projects:
gclient runhooks
.