Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/perl -w
      2 
      3 # Copyright (C) 2011 Adam Barth <abarth (at] webkit.org>
      4 #
      5 # Redistribution and use in source and binary forms, with or without
      6 # modification, are permitted provided that the following conditions
      7 # are met:
      8 # 1. Redistributions of source code must retain the above copyright
      9 #    notice, this list of conditions and the following disclaimer.
     10 # 2. Redistributions in binary form must reproduce the above copyright
     11 #    notice, this list of conditions and the following disclaimer in the
     12 #    documentation and/or other materials provided with the distribution.
     13 #
     14 # THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY
     15 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25 #
     26 
     27 use strict;
     28 
     29 use Config;
     30 use Getopt::Long;
     31 use File::Path;
     32 use File::Spec;
     33 use IO::File;
     34 use InFilesParser;
     35 
     36 require Config;
     37 
     38 package InFilesCompiler;
     39 
     40 my $inputFile = "";
     41 my $outputDir = ".";
     42 my $defaultItemFactory;
     43 
     44 my %parsedItems;
     45 my %parsedItemPaths;
     46 my %parsedParameters;
     47 
     48 sub itemHandler($$$)
     49 {
     50     my ($itemName, $property, $value) = @_;
     51 
     52     if ($itemName =~ /\//) {
     53         my ($dirname, $basename) = $itemName =~ /^(.*)\/(.*)/;
     54         $itemName = $basename;
     55         $parsedItemPaths{$itemName} = $dirname;
     56     }
     57 
     58     $parsedItems{$itemName} = { &$defaultItemFactory($itemName) } if !defined($parsedItems{$itemName});
     59 
     60     return unless $property;
     61 
     62     die "Unknown property $property for $itemName\n" if !defined($parsedItems{$itemName}{$property});
     63     $parsedItems{$itemName}{$property} = $value;
     64 }
     65 
     66 sub parameterHandler($$)
     67 {
     68     my ($parameter, $value) = @_;
     69 
     70     die "Unknown parameter $parameter\n" if !defined($parsedParameters{$parameter});
     71     $parsedParameters{$parameter} = $value;
     72 }
     73 
     74 sub new()
     75 {
     76     my $object = shift;
     77     my $reference = { };
     78 
     79     my $defaultParametersRef = shift;
     80     %parsedParameters = %{ $defaultParametersRef };
     81     $defaultItemFactory = shift;
     82 
     83     %parsedItems = ();
     84 
     85     bless($reference, $object);
     86     return $reference;
     87 }
     88 
     89 sub initializeFromCommandLine()
     90 {
     91     ::GetOptions(
     92         'input=s' => \$inputFile,
     93         'outputDir=s' => \$outputDir,
     94     );
     95 
     96     die "You must specify --input <file>" unless length($inputFile);
     97 
     98     ::mkpath($outputDir);
     99 
    100     # FIXME: Should we provide outputDir via an accessor?
    101     return $outputDir;
    102 }
    103 
    104 sub compile()
    105 {
    106     my $object = shift;
    107     my $generateCode = shift;
    108 
    109     my $file = new IO::File;
    110     open($file, $inputFile) or die "Failed to open file: $!";
    111 
    112     my $InParser = InFilesParser->new();
    113     $InParser->parse($file, \&parameterHandler, \&itemHandler);
    114 
    115     close($file);
    116     die "Failed to read from file: $inputFile" if (keys %parsedItems == 0);
    117 
    118     &$generateCode(\%parsedParameters, \%parsedItems, \%parsedItemPaths);
    119 }
    120 
    121 sub license()
    122 {
    123     return "/*
    124  * THIS FILE WAS AUTOMATICALLY GENERATED, DO NOT EDIT.
    125  *
    126  * Copyright (C) 2011 Google Inc.  All rights reserved.
    127  *
    128  * Redistribution and use in source and binary forms, with or without
    129  * modification, are permitted provided that the following conditions
    130  * are met:
    131  * 1. Redistributions of source code must retain the above copyright
    132  *    notice, this list of conditions and the following disclaimer.
    133  * 2. Redistributions in binary form must reproduce the above copyright
    134  *    notice, this list of conditions and the following disclaimer in the
    135  *    documentation and/or other materials provided with the distribution.
    136  *
    137  * THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY
    138  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    139  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    140  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
    141  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    142  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    143  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    144  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    145  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    146  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    147  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    148  */
    149 
    150 ";
    151 }
    152 
    153 sub interfaceForItem($)
    154 {
    155     my $object = shift;
    156     my $itemName = shift;
    157 
    158     my $interfaceName = $parsedItems{$itemName}{"interfaceName"};
    159     $interfaceName = $itemName unless $interfaceName;
    160 
    161     return $interfaceName;
    162 }
    163 
    164 
    165 sub preferredConditional()
    166 {
    167     my $object = shift;
    168     my $conditional = shift;
    169 
    170     my @conditionals = split('\\|', $conditional);
    171     return $conditionals[0];
    172 }
    173 
    174 sub conditionalStringFromAttributeValue()
    175 {
    176     my $object = shift;
    177     my $conditional = shift;
    178 
    179     return "ENABLE(" . join(') || ENABLE(', split('\\|', $conditional)) . ")";
    180 }
    181 
    182 1;
    183