From 17dc1d6e0ec8f194df3dd74a7033303ad9ff739c Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sun, 12 Jan 2020 21:18:36 +0000 Subject: [PATCH] stdlib: Implement split() function. --- Goalfile.in | 10 +++------- TODO | 3 --- docs/Goalfile.pod | 14 ++++++++++++++ stdlib/prelude.gl | 7 +++++++ tests/10-function-split.expected | 5 +++++ tests/10-function-split.gl | 24 ++++++++++++++++++++++++ tests/10-function-split.sh | 24 ++++++++++++++++++++++++ 7 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 tests/10-function-split.expected create mode 100644 tests/10-function-split.gl create mode 100755 tests/10-function-split.sh diff --git a/Goalfile.in b/Goalfile.in index d28b931..32ff7b5 100644 --- a/Goalfile.in +++ b/Goalfile.in @@ -59,13 +59,9 @@ let MENHIR = "@MENHIR@" let OCAMLDEP = "@OCAMLDEP@" let OCAMLFIND = "@OCAMLFIND@" let OCAMLLEX = "@OCAMLLEX@" -# XXX -let CFLAGS = [ "-g", "-O2", "-I%OCAMLLIB", "-I." ] -#let CFLAGS = "@CFLAGS@ -I%OCAMLLIB -I." -let OCAMLFLAGS = [ "-g", "-safe-string", "-warn-error", "CDEFLMPSUVYZX+52-3" ] -let OCAMLPACKAGES = [ "-package", "str,unix,threads", "-I", "src", "-thread" ] -#let OCAMLFLAGS = "@OCAMLFLAGS@" -#let OCAMLPACKAGES = "@OCAMLPACKAGES@" +let CFLAGS = join (split ("@CFLAGS@"), ["-I%OCAMLLIB", "-I."]) +let OCAMLFLAGS = split ("@OCAMLFLAGS@") +let OCAMLPACKAGES = join (split ("@OCAMLPACKAGES@"), ["-I", "src"]) let objects = [ # These must be in dependency order. diff --git a/TODO b/TODO index eff8cd6..d3ca19b 100644 --- a/TODO +++ b/TODO @@ -21,9 +21,6 @@ Let within functions and goals, eg: { .... } Unclear if this would be helpful or not. -Split "flags" strings. eg. Currently there is no way to pass -$CFLAGS from autoconf into a goalfile. - Make re-execs itself if the Makefile (or any include) changes, and goals should do something similar. See: https://www.gnu.org/software/make/manual/html_node/Remaking-Makefiles.html diff --git a/docs/Goalfile.pod b/docs/Goalfile.pod index f586ec4..abe9f50 100644 --- a/docs/Goalfile.pod +++ b/docs/Goalfile.pod @@ -132,6 +132,20 @@ For example: This takes a list of strings and sorts it, removing duplicates. +=head3 split (string) + +For example: + + split ("-g -O2") ⇒ ["-g", "-O2"] + +Split a string using shell rules into a list of strings. This is +commonly used for splitting C provided by autoconf into a list +for use by goals: + + let CFLAGS = split ("@CFLAGS@") + goal compile (name) = + "%name.o" : "%name.c" { %CC %CFLAGS -c %< -o %@ } + =head3 subst (from, to, text) For example: diff --git a/stdlib/prelude.gl b/stdlib/prelude.gl index 13ac311..04dde93 100644 --- a/stdlib/prelude.gl +++ b/stdlib/prelude.gl @@ -106,6 +106,13 @@ pure function sort (xs) returning strings = @{ for f in %xs; do echo "$f"; done | sort -u } +# Split a string into a list. +# https://superuser.com/a/1066541 +pure function split (s) returning strings = { + s=%s + eval 'for f in '$s'; do echo "$f"; done' +} + # Substitute. pure function subst (from, to, text) returning string = @{ # We need to replace any / characters in ‘to’ with escaped ones. diff --git a/tests/10-function-split.expected b/tests/10-function-split.expected new file mode 100644 index 0000000..cfab676 --- /dev/null +++ b/tests/10-function-split.expected @@ -0,0 +1,5 @@ +-g +-O2 +-I. +-Dvar=hello world +-Dvar2="good bye!" diff --git a/tests/10-function-split.gl b/tests/10-function-split.gl new file mode 100644 index 0000000..0a0b9f9 --- /dev/null +++ b/tests/10-function-split.gl @@ -0,0 +1,24 @@ +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Test the split function. + +let xs = split (" -g -O2 -I. -Dvar=\"hello world\" + -Dvar2=\"\\\"good bye!\\\"\" ") + +goal all = { for f in %xs; do echo "$f"; done } diff --git a/tests/10-function-split.sh b/tests/10-function-split.sh new file mode 100755 index 0000000..c956f3b --- /dev/null +++ b/tests/10-function-split.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +set -e + +goals -f 10-function-split.gl > 10-function-split.out +diff -u 10-function-split.out 10-function-split.expected +rm 10-function-split.out -- 1.8.3.1