Use gyp's rules functionality to compile .coffee/.cson files

Instead of finding and compiling all .coffee/.cson files in
script/copy-files-to-bundle, we now tell gyp how to do this for us. It
works like this:

1. Rakefile invokes the new script/generate-sources-gypi script to
   generate sources.gypi. This file lists all the .coffee/.cson files in
   the src, static, and vendor directories, as well as a new
   compiled_sources_dir variable that specifies where the compiled
   versions of the files should be placed.
2. atom.gyp includes sources.gypi.
3. atom.gyp has a new target, generated_sources, which contains all the
   .coffee/.cson files, and uses two rules to tell gyp how to compile
   them. The rules invoke the new script/compile-coffee and
   script/compile-cson files once for each file.
4. gyp generates one Makefile for each rule to actually perform the
   compilation.
5. script/copy-files-to-bundle now takes the compiled_sources_dir
   variable as an argument, and copies files both from there and from
   the repository into the Resources directory.

By putting the compilation into a different target, we can do it in
parallel with compiling/linking our binaries. And gyp automatically runs
make using -j$(sysctl -n hw.ncpu), so compilation of .coffee/.cson files
happens in parallel, too.

These changes reduce clean build time on my MacBook Pro from 55 seconds
to 46 seconds.
This commit is contained in:
Adam Roben
2013-03-04 11:32:06 -05:00
parent 6511d0e111
commit 83ee2d23b3
7 changed files with 116 additions and 36 deletions

1
.gitignore vendored
View File

@@ -9,3 +9,4 @@ node_modules
npm-debug.log
/tags
/cef/
/sources.gypi

View File

@@ -14,6 +14,7 @@ end
desc "Create xcode project from gyp file"
task "create-xcode-project" => "update-cef" do
`rm -rf atom.xcodeproj`
`script/generate-sources-gypi`
`gyp --depth=. -D CODE_SIGN="#{ENV['CODE_SIGN']}" atom.gyp`
end

View File

@@ -36,6 +36,7 @@
'includes': [
'cef/cef_paths2.gypi',
'git2/libgit2.gypi',
'sources.gypi',
],
'target_defaults': {
'default_configuration': 'Debug',
@@ -214,6 +215,7 @@
'type': 'shared_library',
'mac_bundle': 1,
'dependencies': [
'generated_sources',
'libcef_dll_wrapper',
],
'defines': [
@@ -276,9 +278,10 @@
],
'postbuilds': [
{
'postbuild_name': 'Copy and Compile Static Files',
'postbuild_name': 'Copy Static Files',
'action': [
'script/copy-files-to-bundle'
'script/copy-files-to-bundle',
'<(compiled_sources_dir_xcode)',
],
},
{
@@ -311,6 +314,48 @@
],
}
},
{
'target_name': 'generated_sources',
'type': 'none',
'sources': [
'<@(coffee_sources)',
'<@(cson_sources)',
],
'rules': [
{
'rule_name': 'coffee',
'extension': 'coffee',
'inputs': [
'script/compile-coffee',
],
'outputs': [
'<(compiled_sources_dir)/<(RULE_INPUT_DIRNAME)/<(RULE_INPUT_ROOT).js',
],
'action': [
'sh',
'script/compile-coffee',
'<(RULE_INPUT_PATH)',
'<(compiled_sources_dir)/<(RULE_INPUT_DIRNAME)/<(RULE_INPUT_ROOT).js',
],
},
{
'rule_name': 'cson2json',
'extension': 'cson',
'inputs': [
'script/compile-cson',
],
'outputs': [
'<(compiled_sources_dir)/<(RULE_INPUT_DIRNAME)/<(RULE_INPUT_ROOT).json',
],
'action': [
'sh',
'script/compile-cson',
'<(RULE_INPUT_PATH)',
'<(compiled_sources_dir)/<(RULE_INPUT_DIRNAME)/<(RULE_INPUT_ROOT).json',
],
},
],
},
],
'conditions': [
['os_posix==1 and OS!="mac" and OS!="android" and gcc_version==46', {

16
script/compile-coffee Executable file
View File

@@ -0,0 +1,16 @@
#!/bin/sh
set -e
# Because of the way xcodebuild invokes external scripts we need to load
# The Setup's environment ourselves. If this isn't done, things like the
# node shim won't be able to find the stuff they need.
if [ -e /opt/github/env.sh ]; then
source /opt/github/env.sh
fi
INPUT_FILE="${1}"
OUTPUT_FILE="${2}"
node_modules/.bin/coffee -c -p "${INPUT_FILE}" > "${OUTPUT_FILE}"

16
script/compile-cson Executable file
View File

@@ -0,0 +1,16 @@
#!/bin/sh
set -e
# Because of the way xcodebuild invokes external scripts we need to load
# The Setup's environment ourselves. If this isn't done, things like the
# node shim won't be able to find the stuff they need.
if [ -e /opt/github/env.sh ]; then
source /opt/github/env.sh
fi
INPUT_FILE="${1}"
OUTPUT_FILE="${2}"
node_modules/.bin/cson2json "${INPUT_FILE}" > "${OUTPUT_FILE}"

View File

@@ -1,41 +1,10 @@
#!/bin/sh
# This can only be run by xcode or xcodebuild!
# Because of the way xcodebuild invokes external scripts we need to load
# The Setup's environment ourselves. If this isn't done, things like the
# node shim won't be able to find the stuff they need.
if [ -f /opt/github/env.sh ]; then
source /opt/github/env.sh
fi
set -e
COMPILED_SOURCES_DIR="${1}"
RESOUCES_PATH="$BUILT_PRODUCTS_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH"
DIRS="src static vendor"
# Compile .coffee files into bundle
COFFEE_FILES=$(find $DIRS -type file -name '*.coffee')
for COFFEE_FILE in $COFFEE_FILES; do
JS_FILE=$(echo "$RESOUCES_PATH/$COFFEE_FILE" | sed 's/.coffee/.js/' )
OUTPUT_PATH="$RESOUCES_PATH/$(dirname "$COFFEE_FILE")"
if [ $COFFEE_FILE -nt "$JS_FILE" ]; then
mkdir -p "$OUTPUT_PATH"
node_modules/.bin/coffee -c -o "$OUTPUT_PATH" "$COFFEE_FILE" || exit 1
fi
done;
# Compile .cson files into bundle
CSON_FILES=$(find $DIRS -type file -name '*.cson')
for CSON_FILE in $CSON_FILES; do
JSON_FILE=$(echo "$RESOUCES_PATH/$CSON_FILE" | sed 's/.cson/.json/' )
OUTPUT_PATH="$RESOUCES_PATH/$(dirname "$CSON_FILE")"
if [ $CSON_FILE -nt "$JSON_FILE" ]; then
mkdir -p "$OUTPUT_PATH"
node_modules/.bin/cson2json "$CSON_FILE" > "$JSON_FILE"
fi
done;
# Copy non-coffee files into bundle
rsync --archive --recursive --exclude="src/**/*.coffee" --exclude="src/**/*.cson" src static vendor spec benchmark themes dot-atom atom.sh "$RESOUCES_PATH"
rsync --archive --recursive --exclude="src/**/*.coffee" --exclude="src/**/*.cson" src static vendor spec benchmark themes dot-atom atom.sh "${COMPILED_SOURCES_DIR}/" "$RESOUCES_PATH"

32
script/generate-sources-gypi Executable file
View File

@@ -0,0 +1,32 @@
#!/bin/sh
set -e
cd "$(dirname $0)/.."
DIRS="src static vendor"
find_files() {
find ${DIRS} -type file -name ${1}
}
file_list() {
while read file; do
echo " '${file}',"
done
}
cat > sources.gypi <<EOF
{
'variables': {
'compiled_sources_dir': '<(INTERMEDIATE_DIR)/atom-resources',
'compiled_sources_dir_xcode': '\${INTERMEDIATE_DIR}/atom-resources',
'coffee_sources': [
$(find_files '*.coffee' | file_list)
],
'cson_sources': [
$(find_files '*.cson' | file_list)
],
},
}
EOF