Home | History | Annotate | Download | only in gyp
      1 #!/usr/bin/env python
      2 # Copyright (c) 2011 Google Inc. All rights reserved.
      3 #
      4 # Redistribution and use in source and binary forms, with or without
      5 # modification, are permitted provided that the following conditions are
      6 # met:
      7 #
      8 #     * Redistributions of source code must retain the above copyright
      9 # notice, this list of conditions and the following disclaimer.
     10 #     * Redistributions in binary form must reproduce the above
     11 # copyright notice, this list of conditions and the following disclaimer
     12 # in the documentation and/or other materials provided with the
     13 # distribution.
     14 #     * Neither the name of Google Inc. nor the names of its
     15 # contributors may be used to endorse or promote products derived from
     16 # this software without specific prior written permission.
     17 #
     18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 
     30 
     31 import os
     32 import subprocess
     33 
     34 from optparse import OptionParser
     35 
     36 def chdir_to_source():
     37     source_directory = os.path.abspath(os.path.join(__file__, '..', '..'))
     38     os.chdir(source_directory)
     39 
     40 def gyp():
     41     return os.path.join('ThirdParty', 'gyp', 'gyp')
     42 
     43 
     44 class Project:
     45     def __init__(self, name):
     46         self._name = name
     47 
     48     def name(self):
     49         return self._name
     50 
     51     def _gyp_directory(self):
     52         return os.path.join(self._name, 'gyp')
     53 
     54     def _gyp_file_for_port(self, port):
     55         # Gyp uses the gyp file name as the XCode proj file name, so for now "apple-mac" must be ProjectName.gyp
     56         if port == "mac":
     57             return '%s.gyp' % self._name
     58         return "%s.gyp" % port
     59 
     60     def inputs(self, port):
     61         return [
     62             os.path.join(self._gyp_directory(), self._gyp_file_for_port(port)),
     63             os.path.join(self._name, self._name + '.gypi'),
     64             os.path.join('gyp', 'common.gypi'),
     65         ]
     66 
     67     def _output_for_port(self, port):
     68         format = format_for_port(port)
     69         return {
     70             'filelist': '%s.am' % port,
     71             'xcode': os.path.join(self._name + '.xcodeproj', 'project.pbxproj'),
     72         }[format]
     73 
     74     def output(self, port):
     75         return os.path.join(self._gyp_directory(), self._output_for_port(port))
     76 
     77     def should_generate(self, port):
     78         if not os.path.exists(self.output(port)):
     79             return True
     80         return os.path.getmtime(self.output(port)) < self._newest(self.inputs(port))
     81 
     82     def _extra_args_for_format(self, format):
     83         if format == "xcode":
     84             return ['-G', 'xcode_list_excluded_files=0']
     85         return []
     86 
     87     def generate(self, port):
     88         args = [
     89             gyp(),
     90             self.inputs(port)[0],
     91             '--depth=.',
     92         ]
     93         format = format_for_port(port)
     94         args.append('--format=%s' % format)
     95         args += self._extra_args_for_format(format)
     96 
     97         subprocess.call(args)
     98         # GYP doesn't always touch the output file, but we want to touch the
     99         # file so that we don't keep trying to regenerate it.
    100         os.utime(self.output(port), None)
    101 
    102     @staticmethod
    103     def _newest(paths):
    104         return max([os.path.getmtime(path) for path in paths])
    105 
    106 
    107 def format_for_port(port):
    108     return {
    109         'mac': 'xcode',
    110         'gtk': 'filelist',
    111         'win': 'msvs',
    112     }[port] # Valid port is required.
    113 
    114 
    115 PROJECTS = [
    116     Project("JavaScriptCore"),
    117     Project("WebCore"),
    118 ]
    119 
    120 def projects_to_generate(port):
    121     should_generate = [project for project in PROJECTS if project.should_generate(port)]
    122     already_generated = [project.name() for project in set(PROJECTS) - set(should_generate)]
    123 
    124     if already_generated:
    125         print "Not generating %s because the generated files exist and are newer than the GYP files." % ', '.join(already_generated)
    126         print "Pass --regenerate-projects to override."
    127 
    128     return should_generate
    129 
    130 
    131 def main():
    132     chdir_to_source()
    133 
    134     parser = OptionParser()
    135     parser.add_option("--port", dest="port", action="store", default="mac", # Default to Mac for now
    136                       help="Which port to generate for.")
    137     parser.add_option("--regenerate-projects", dest="regenerate_projects",
    138                       default=False, action="store_true",
    139                       help="Generate all project files even if they appear to be up to date.")
    140     (options, args) = parser.parse_args()
    141 
    142     projects = PROJECTS
    143     if not options.regenerate_projects:
    144         projects = projects_to_generate(options.port)
    145 
    146     for project in projects:
    147         print "Generating %s." % project.name()
    148         project.generate(options.port)
    149 
    150 
    151 if __name__ == "__main__":
    152     main()
    153