Skip to content

Commit

Permalink
[Truffle] Implement the options generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Nov 30, 2016
1 parent 0617393 commit adddfdb
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 9 deletions.
5 changes: 4 additions & 1 deletion tool/jt.rb
Expand Up @@ -444,6 +444,7 @@ def help
truffle build only the Truffle part, assumes the rest is up-to-date
cexts [--no-openssl] build the cext backend (set SULONG_HOME)
parser build the parser
options build the options
--build-pack use the build pack
--offline use the build pack to build offline
jt clean clean
Expand Down Expand Up @@ -552,6 +553,8 @@ def build(*options)
sh 'sh', 'tool/truffle/generate_parser'
yytables = 'truffle/src/main/java/org/jruby/truffle/parser/parser/YyTables.java'
File.write(yytables, File.read(yytables).gsub('package org.jruby.parser;', 'package org.jruby.truffle.parser.parser;'))
when 'options'
sh 'tool/truffle/generate-options.rb'
when nil
mvn env, *maven_options, 'package'
else
Expand Down Expand Up @@ -1335,7 +1338,7 @@ def main(args)
send(args.shift)
when "build"
command = [args.shift]
while ['truffle', 'cexts', 'parser', '--offline', '--build-pack', '--no-openssl'].include?(args.first)
while ['truffle', 'cexts', 'parser', 'options', '--offline', '--build-pack', '--no-openssl'].include?(args.first)
command << args.shift
end
send(*command)
Expand Down
108 changes: 108 additions & 0 deletions tool/truffle/generate-options.rb
@@ -0,0 +1,108 @@
#!/usr/bin/env ruby

# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved.
# This code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require 'ostruct'
require 'yaml'
require 'erb'

options = YAML.load_file('truffle/src/main/java/org/jruby/truffle/options/options.yml')

options = options.map do |constant, (name, type, default, description)|
case type
when 'boolean'
type = 'boolean'
type_cons = 'BooleanOptionDescription'
default = default.to_s
when 'string-array'
type = 'String[]'
type_cons = 'StringArrayOptionDescription'
default = "new String[]{#{default.map(&:inspect).join(', ')}}"
else
raise type
end

OpenStruct.new(
:constant => constant,
:name => name,
:type => type,
:default => default,
:description => description,
:type_cons => type_cons
)
end

File.write('truffle/src/main/java/org/jruby/truffle/options/NewOptions.java', ERB.new(%{/*
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.options;
// This file is automatically generated by options.yml with 'jt build options'
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import javax.annotation.Generated;
@Generated("tool/truffle/generate-options.rb")
public class NewOptions {
<% options.each do |o| %>
<% if o.type.end_with?('[]') %>@CompilationFinal(dimensions=1) <% end %>public final <%= o.type %> <%= o.constant %>;
<% end %>
NewOptions(OptionsBuilder builder) {
<% options.each do |o| %>
<%= o.constant %> = builder.getOrDefault(OptionsCatalog.<%= o.constant %>);
<% end %>
}
}
}).result)

File.write('truffle/src/main/java/org/jruby/truffle/options/OptionsCatalog.java', ERB.new(%{/*
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.options;
// This file is automatically generated by options.yml with 'jt build options'
import javax.annotation.Generated;
@Generated("tool/truffle/generate-options.rb")
public class OptionsCatalog {
<% options.each do |o| %>
public static final OptionDescription <%= o.constant %> = new <%= o.type_cons %>("<%= o.name %>", "<%= o.description %>", <%= o.default %>);
<% end %>
public static OptionDescription fromName(String name) {
switch (name) {
<% options.each do |o| %>
case "<%= o.name %>":
return <%= o.constant %>;
<% end %>
default:
return null;
}
}
}
}).result)
12 changes: 10 additions & 2 deletions truffle/src/main/java/org/jruby/truffle/options/NewOptions.java
Expand Up @@ -9,19 +9,27 @@
*/
package org.jruby.truffle.options;

// This file would be automatically generated from the list of options in the text file.
// This file is automatically generated by options.yml with 'jt build options'

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;

import javax.annotation.Generated;

@Generated("tool/truffle/generate-options.rb")
public class NewOptions {


@CompilationFinal(dimensions=1) public final String[] ARGUMENTS;

public final boolean EXCEPTIONS_PRINT_JAVA;


NewOptions(OptionsBuilder builder) {

ARGUMENTS = builder.getOrDefault(OptionsCatalog.ARGUMENTS);

EXCEPTIONS_PRINT_JAVA = builder.getOrDefault(OptionsCatalog.EXCEPTIONS_PRINT_JAVA);

}

}
Expand Up @@ -9,20 +9,28 @@
*/
package org.jruby.truffle.options;

// This file would be automatically generated from the list of options in the text file.
// This file is automatically generated by options.yml with 'jt build options'

public class OptionsCatalog {
import javax.annotation.Generated;

public static final OptionDescription ARGUMENTS = new StringArrayOptionDescription("arguments", "Foo bar baz", new String[0]);
@Generated("tool/truffle/generate-options.rb")
public class OptionsCatalog {

public static final OptionDescription EXCEPTIONS_PRINT_JAVA = new BooleanOptionDescription("exceptions.print_java", "Foo baz bar", false);

public static final OptionDescription ARGUMENTS = new StringArrayOptionDescription("arguments", "Command line arguments for the Ruby program", new String[]{});

public static final OptionDescription EXCEPTIONS_PRINT_JAVA = new BooleanOptionDescription("exceptions.print_java", "Print Java exceptions at the point of translating them to Ruby exceptions", false);


public static OptionDescription fromName(String name) {
switch (name) {

case "arguments":
return ARGUMENTS;

case "exceptions.print_java":
return EXCEPTIONS_PRINT_JAVA;

default:
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions truffle/src/main/java/org/jruby/truffle/options/options.yml
@@ -1,2 +1,2 @@
ARGUMENTS: [arguments, string-array, [], Foo bar baz]
EXCEPTIONS_PRINT_JAVA: [exceptions.print_java, boolean, false, Foo bar baz]
ARGUMENTS: [arguments, string-array, [], Command line arguments for the Ruby program]
EXCEPTIONS_PRINT_JAVA: [exceptions.print_java, boolean, false, Print Java exceptions at the point of translating them to Ruby exceptions]

0 comments on commit adddfdb

Please sign in to comment.