Skip to content

Commit

Permalink
target out_dir in build script
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Thom committed Sep 27, 2019
1 parent 9a83ccb commit 5ec4dfe
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
18 changes: 13 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ extern crate indexmap;

use indexmap::IndexSet;

use std::fs::{File, read_dir};
use std::env;
use std::fs::{File, copy, read_dir};
use std::io::Write;
use std::path::Path;

fn main()
{
let dest_path = Path::new("./src/prolog/machine/libraries.rs");
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("libraries.rs");

let mut libraries = File::create(&dest_path).unwrap();
let mut library_index = IndexSet::new();
Expand All @@ -18,14 +20,20 @@ fn main()
for item in paths {
let item = item.unwrap().path();

if item.is_file() {
if let Some(file_name) = item.file_name() {
if let Some(ext) = item.extension() {
if ext == "pl" {
let file_stem = item.file_stem().unwrap();
let file_str = file_stem.to_string_lossy().to_uppercase();
let dest = Path::new(&out_dir).join(file_name);

let include_line = format!("static {}: &str = include_str!(\"{}/{}.pl\");\n",
file_str, "../lib", file_stem.to_string_lossy());
match copy(&item, dest) {
Ok(_) => {},
Err(e) => panic!("die: {:?}", e)
};

let include_line = format!("static {}: &str = include_str!(\"{}.pl\");\n",
file_str, file_stem.to_string_lossy());

libraries.write_all(include_line.as_bytes()).unwrap();
library_index.insert(file_stem.to_string_lossy().to_string());
Expand Down
3 changes: 1 addition & 2 deletions src/prolog/machine/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,7 @@ impl ListingCompiler {
&Declaration::Hook(hook, _, ref queue) if !hook.has_module_scope() => {
worker.term_stream.incr_expansion_lens(hook, 1, queue.len())
}
&Declaration::UseModule(ModuleSource::File(_))
| &Declaration::UseQualifiedModule(ModuleSource::File(_), _) => {
&Declaration::UseModule(_) | &Declaration::UseQualifiedModule(..) => {
update_expansion_lengths = true
}
_ => {}
Expand Down
2 changes: 1 addition & 1 deletion src/prolog/machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl SubModuleUser for IndexStore {
}
}

include!("libraries.rs");
include!(concat!(env!("OUT_DIR"), "/libraries.rs"));

static TOPLEVEL: &str = include_str!("../toplevel.pl");

Expand Down
25 changes: 12 additions & 13 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,13 +1790,13 @@ fn test_queries_on_conditionals() {
fn test_queries_on_modules() {
let mut wam = Machine::new(readline::input_stream());

submit(&mut wam, ":- use_module('src/prolog/lib/lists.pl').");
submit(&mut wam, ":- use_module(library(lists)).");

submit_code(
&mut wam,
"
:- module(my_lists, [local_member/2, reverse/2]).
:- use_module('src/prolog/lib/lists.pl', [member/2]).
:- use_module(library(lists), [member/2]).
local_member(X, Xs) :- member(X, Xs).
Expand Down Expand Up @@ -1826,7 +1826,7 @@ reverse(Xs, Ys) :- lists:reverse(Xs, Ys).",

submit(
&mut wam,
":- use_module('src/prolog/lib/lists.pl', [reverse/2]).",
":- use_module(library(lists), [reverse/2]).",
);

assert_prolog_success!(
Expand All @@ -1836,7 +1836,7 @@ reverse(Xs, Ys) :- lists:reverse(Xs, Ys).",
);
assert_prolog_success!(&mut wam, "reverse(_, _).");

submit(&mut wam, ":- use_module('src/prolog/lib/lists.pl', []).");
submit(&mut wam, ":- use_module(library(lists), []).");

assert_prolog_success!(
&mut wam,
Expand All @@ -1849,7 +1849,7 @@ reverse(Xs, Ys) :- lists:reverse(Xs, Ys).",
fn test_queries_on_builtins() {
let mut wam = Machine::new(readline::input_stream());

submit(&mut wam, ":- use_module('src/prolog/lib/lists.pl').");
submit(&mut wam, ":- use_module(library(lists)).");

assert_prolog_failure!(&mut wam, "atom(X).");
assert_prolog_success!(&mut wam, "atom(a).");
Expand Down Expand Up @@ -2113,7 +2113,7 @@ fn test_queries_on_builtins() {
assert_prolog_success!(&mut wam, "1.0 @=< 1.");
assert_prolog_success!(&mut wam, "1 @=< 1.0.");

submit(&mut wam, ":- use_module('src/prolog/lib/non_iso.pl').");
submit(&mut wam, ":- use_module(library(non_iso)).");

assert_prolog_success!(&mut wam, "variant(X, Y).");
assert_prolog_failure!(&mut wam, "variant(f(X), f(x)).");
Expand Down Expand Up @@ -2696,7 +2696,7 @@ foo(X) :- call(X) -> call(X).",
fn test_queries_on_setup_call_cleanup() {
let mut wam = Machine::new(readline::input_stream());

submit(&mut wam, ":- use_module('src/prolog/lib/non_iso.pl').");
submit(&mut wam, ":- use_module(library(non_iso)).");

// Test examples from the ISO Prolog page for setup_call_catch.
assert_prolog_failure!(&mut wam, "setup_call_cleanup(false, _, _).");
Expand Down Expand Up @@ -2805,7 +2805,7 @@ fn test_queries_on_setup_call_cleanup() {
fn test_queries_on_call_with_inference_limit() {
let mut wam = Machine::new(readline::input_stream());

submit(&mut wam, ":- use_module('src/prolog/lib/non_iso.pl').");
submit(&mut wam, ":- use_module(library(non_iso)).");

assert_prolog_success!(
&mut wam,
Expand Down Expand Up @@ -3027,7 +3027,7 @@ fn test_queries_on_call_with_inference_limit() {
fn test_queries_on_dcgs() {
let mut wam = Machine::new(readline::input_stream());

submit(&mut wam, ":- use_module('src/prolog/lib/dcgs.pl').");
submit(&mut wam, ":- use_module(library(dcgs)).");

// test case by YeGoblynQueene from hacker news.
submit_code(
Expand Down Expand Up @@ -3064,7 +3064,7 @@ fn test_queries_on_dcgs() {
fn test_queries_on_string_lists() {
let mut wam = Machine::new(readline::input_stream());

submit(&mut wam, ":- use_module('src/prolog/lib/non_iso.pl').");
submit(&mut wam, ":- use_module(library(non_iso)).");

// double_quotes is chars by default.
assert_prolog_success!(&mut wam, "variant(\"\", []).");
Expand Down Expand Up @@ -3426,15 +3426,14 @@ fn test_queries_on_attributed_variables() {
&mut wam,
"
:- module(my_mod, []).
:- use_module('src/prolog/lib/atts.pl').
:- use_module(library(atts)).
:- attribute dif/1, frozen/1.",
);

assert_prolog_success!(
&mut wam,
"( put_atts(V, my_mod, dif(1)) ; put_atts(V, my_mod, dif(2)) ),
get_atts(V, my_mod, L).",
"( put_atts(V, my_mod, dif(1)) ; put_atts(V, my_mod, dif(2)) ), get_atts(V, my_mod, L).",
[["L = [dif(1)]", "V = _10"], ["L = [dif(2)]", "V = _10"]]
);

Expand Down

0 comments on commit 5ec4dfe

Please sign in to comment.