diff mbox series

[3/7] meson: fix nativesdk-meson for multilib SDKs

Message ID 20190107163823.31816-3-ross.burton@intel.com
State Accepted
Commit 8596f871ef834a38e3375443f7eb08e43816347a
Headers show
Series [1/7] toolchain-scripts: run post-relocate scripts for every environment | expand

Commit Message

Ross Burton Jan. 7, 2019, 4:38 p.m. UTC
Multilib SDKs differ only in the environment variables set, so nativesdk-meson's
setup script needs to write a cross file for each environment.

Rename the shipped meson.cross to meson.cross.template, as it cannot be used
directly.  Now that post-relocate scripts are called once for each environment,
the generated meson.cross can be prefixed with TARGET_PREFIX to ensure it is
unique.

Finally rewrite the setup script to use string.Template to perform the expansion
instead of hand-coding the logic.

Signed-off-by: Ross Burton <ross.burton@intel.com>

---
 meta/recipes-devtools/meson/meson/meson-setup.py   | 69 ++++++----------------
 meta/recipes-devtools/meson/meson/meson-wrapper    |  2 +-
 .../meson/nativesdk-meson_0.49.0.bb                | 41 +++++--------
 3 files changed, 36 insertions(+), 76 deletions(-)

-- 
2.11.0

-- 
_______________________________________________
Openembedded-core mailing list
Openembedded-core@lists.openembedded.org
http://lists.openembedded.org/mailman/listinfo/openembedded-core

Comments

Martin Kelly Jan. 7, 2019, 7:03 p.m. UTC | #1
On 1/7/19 8:38 AM, Ross Burton wrote:
> Multilib SDKs differ only in the environment variables set, so nativesdk-meson's

> setup script needs to write a cross file for each environment.

> 

> Rename the shipped meson.cross to meson.cross.template, as it cannot be used

> directly.  Now that post-relocate scripts are called once for each environment,

> the generated meson.cross can be prefixed with TARGET_PREFIX to ensure it is

> unique.

> 

> Finally rewrite the setup script to use string.Template to perform the expansion

> instead of hand-coding the logic.

> 

> Signed-off-by: Ross Burton <ross.burton@intel.com>

> ---

>   meta/recipes-devtools/meson/meson/meson-setup.py   | 69 ++++++----------------

>   meta/recipes-devtools/meson/meson/meson-wrapper    |  2 +-

>   .../meson/nativesdk-meson_0.49.0.bb                | 41 +++++--------

>   3 files changed, 36 insertions(+), 76 deletions(-)

> 

> diff --git a/meta/recipes-devtools/meson/meson/meson-setup.py b/meta/recipes-devtools/meson/meson/meson-setup.py

> index a9749eae9d4..808e2a062f6 100755

> --- a/meta/recipes-devtools/meson/meson/meson-setup.py

> +++ b/meta/recipes-devtools/meson/meson/meson-setup.py

> @@ -1,62 +1,31 @@

>   #!/usr/bin/env python3

>   

>   import os

> +import string

>   import sys

>   

> -def bail(msg):

> -    print(msg, file=sys.stderr)

> -    sys.exit(1)

> -

> -_MARKER = '@@'

> -def transform_line(line):

> -    # Substitute any special markers of this form:

> -    # @@ENV@@

> -    # with the value of ENV, split into meson array syntax.

> -    start = line.find(_MARKER)

> -    if start == -1:

> -        return line

> -

> -    end = line.rfind(_MARKER)

> -    if end == start:

> -        return line

> -

> -    # Lookup value of the env var.

> -    var = line[start+len(_MARKER):end]

> -    try:

> -        val = os.environ[var]

> -    except KeyError:

> -        bail('cannot generate meson.cross; env var %s not set' % var)

> +class Template(string.Template):

> +    delimiter = "@"

>   

> -    # Transform into meson array.

> -    val = ["'%s'" % x for x in val.split()]

> -    val = ', '.join(val)

> -    val = '[%s]' % val

> +class Environ():

> +    def __getitem__(self, name):

> +        val = os.environ[name]

> +        val = ["'%s'" % x for x in val.split()]

> +        val = ', '.join(val)

> +        val = '[%s]' % val

> +        return val

>   

> -    before = line[:start]

> -    after = line[end+len(_MARKER):]

> -

> -    return '%s%s%s' % (before, val, after)

> -

> -# Make sure this is really an SDK extraction environment.

>   try:

>       sysroot = os.environ['OECORE_NATIVE_SYSROOT']

>   except KeyError:

> -    bail('OECORE_NATIVE_SYSROOT env var must be set')

> -

> -cross_file = os.path.join(sysroot, 'usr/share/meson/meson.cross')

> -tmp_cross_file = '%s.tmp' % cross_file

> +    print("Not in environment setup, bailing")


This should print to sys.stderr as the bail function did.

> +    sys.exit(1)

>   

> -# Read through and transform the current meson.cross.

> -lines = []

> -with open(cross_file, 'r') as f:

> -    for line in f:

> -        lines.append(transform_line(line))

> +template_file = os.path.join(sysroot, 'usr/share/meson/meson.cross.template')

> +cross_file = os.path.join(sysroot, 'usr/share/meson/%smeson.cross' % os.environ["TARGET_PREFIX"])

>   

> -# Write the transformed result to a tmp file and atomically rename it. In case

> -# we crash during the file write, we don't want an invalid meson.cross file.

> -with open(tmp_cross_file, 'w') as f:

> -    for line in lines:

> -        f.write(line)

> -    f.flush()

> -    os.fdatasync(f.fileno())

> -os.rename(tmp_cross_file, cross_file)

> +with open(template_file) as in_file:

> +    template = in_file.read()

> +    output = Template(template).substitute(Environ())

> +    with open(cross_file, "w") as out_file:

> +        out_file.write(output)


I like using string.Template instead of custom logic, but shouldn't we 
keep the atomic rename logic? If someone hits control-C or the 
extraction process crashes during the write, we don't want a corrupt 
file due to a partial write.

> diff --git a/meta/recipes-devtools/meson/meson/meson-wrapper b/meta/recipes-devtools/meson/meson/meson-wrapper

> index b2e00da513f..d4ffe60f9a1 100755

> --- a/meta/recipes-devtools/meson/meson/meson-wrapper

> +++ b/meta/recipes-devtools/meson/meson/meson-wrapper

> @@ -10,5 +10,5 @@ fi

>   unset CC CXX CPP LD AR NM STRIP

>   

>   exec "$OECORE_NATIVE_SYSROOT/usr/bin/meson.real" \

> -     --cross-file "$OECORE_NATIVE_SYSROOT/usr/share/meson/meson.cross" \

> +     --cross-file "${OECORE_NATIVE_SYSROOT}/usr/share/meson/${TARGET_PREFIX}meson.cross" \

>        "$@"

> diff --git a/meta/recipes-devtools/meson/nativesdk-meson_0.49.0.bb b/meta/recipes-devtools/meson/nativesdk-meson_0.49.0.bb

> index 721ee8c8f12..55c57775e06 100644

> --- a/meta/recipes-devtools/meson/nativesdk-meson_0.49.0.bb

> +++ b/meta/recipes-devtools/meson/nativesdk-meson_0.49.0.bb

> @@ -5,9 +5,6 @@ inherit nativesdk

>   SRC_URI += "file://meson-setup.py \

>               file://meson-wrapper"

>   

> -def meson_array(var, d):

> -    return "', '".join(d.getVar(var).split()).join(("'", "'"))

> -

>   # both are required but not used by meson

>   MESON_SDK_ENDIAN = "bogus-endian"

>   MESON_TARGET_ENDIAN = "bogus-endian"

> @@ -17,32 +14,31 @@ MESON_C_ARGS = "${MESON_TOOLCHAIN_ARGS} ${BUILDSDK_CFLAGS}"

>   MESON_CPP_ARGS = "${MESON_TOOLCHAIN_ARGS} ${BUILDSDK_CXXFLAGS}"

>   MESON_LINK_ARGS = "${MESON_TOOLCHAIN_ARGS} ${BUILDSDK_LDFLAGS}"

>   

> -# This logic is similar but not identical to that in meson.bbclass, since it's

> -# generating for an SDK rather than a cross-compile. Important differences are:

> +# The cross file logic is similar but not identical to that in meson.bbclass,

> +# since it's generating for an SDK rather than a cross-compile. Important

> +# differences are:

>   # - We can't set vars like CC, CXX, etc. yet because they will be filled in with

>   #   real paths by meson-setup.sh when the SDK is extracted.

>   # - Some overrides aren't needed, since the SDK injects paths that take care of

>   #   them.

> -addtask write_config before do_install

> -do_write_config[vardeps] += "MESON_C_ARGS MESON_CPP_ARGS MESON_LINK_ARGS CC CXX LD AR NM STRIP READELF"

> -do_write_config() {

> -    # This needs to be Py to split the args into single-element lists

> -    cat >${WORKDIR}/meson.cross <<EOF

> +do_install_append() {

> +    install -d ${D}${datadir}/meson

> +    cat >${D}${datadir}/meson/meson.cross.template <<EOF

>   [binaries]

> -c = @@CC@@

> -cpp = @@CXX@@

> -ar = @@AR@@

> -nm = @@NM@@

> -ld = @@LD@@

> -strip = @@STRIP@@

> +c = @CC

> +cpp = @CXX

> +ar = @AR

> +nm = @NM

> +ld = @LD

> +strip = @STRIP

>   pkgconfig = 'pkg-config'

>   

>   [properties]

>   needs_exe_wrapper = true

> -c_args = @@CFLAGS@@

> -c_link_args = @@LDFLAGS@@

> -cpp_args = @@CPPFLAGS@@

> -cpp_link_args = @@LDFLAGS@@

> +c_args = @CFLAGS

> +c_link_args = @LDFLAGS

> +cpp_args = @CPPFLAGS

> +cpp_link_args = @LDFLAGS

>   


I have no problem with it, but I'm curious why we use @CC instead of 
@@CC@@ style here. Is there an issue with @@CC@@ style?

>   [host_machine]

>   system = '${SDK_OS}'

> @@ -50,11 +46,6 @@ cpu_family = '${SDK_ARCH}'

>   cpu = '${SDK_ARCH}'

>   endian = '${MESON_SDK_ENDIAN}'

>   EOF

> -}

> -

> -do_install_append() {

> -    install -d ${D}${datadir}/meson

> -    install -m 0644 ${WORKDIR}/meson.cross ${D}${datadir}/meson/

>   

>       install -d ${D}${SDKPATHNATIVE}/post-relocate-setup.d

>       install -m 0755 ${WORKDIR}/meson-setup.py ${D}${SDKPATHNATIVE}/post-relocate-setup.d/

> 

-- 
_______________________________________________
Openembedded-core mailing list
Openembedded-core@lists.openembedded.org
http://lists.openembedded.org/mailman/listinfo/openembedded-core
Ross Burton Jan. 7, 2019, 8:02 p.m. UTC | #2
On Mon, 7 Jan 2019 at 19:03, Martin Kelly <mkelly@xevo.com> wrote:
> > +with open(template_file) as in_file:

> > +    template = in_file.read()

> > +    output = Template(template).substitute(Environ())

> > +    with open(cross_file, "w") as out_file:

> > +        out_file.write(output)

>

> I like using string.Template instead of custom logic, but shouldn't we

> keep the atomic rename logic? If someone hits control-C or the

> extraction process crashes during the write, we don't want a corrupt

> file due to a partial write.


If someone control-c's during SDK unpack, the SDK isn't usable.

> I have no problem with it, but I'm curious why we use @CC instead of

> @@CC@@ style here. Is there an issue with @@CC@@ style?


Because that's how string.Template works.

Ross
-- 
_______________________________________________
Openembedded-core mailing list
Openembedded-core@lists.openembedded.org
http://lists.openembedded.org/mailman/listinfo/openembedded-core
Martin Kelly Jan. 7, 2019, 8:07 p.m. UTC | #3
On 1/7/19 12:02 PM, Burton, Ross wrote:
> On Mon, 7 Jan 2019 at 19:03, Martin Kelly <mkelly@xevo.com> wrote:

>>> +with open(template_file) as in_file:

>>> +    template = in_file.read()

>>> +    output = Template(template).substitute(Environ())

>>> +    with open(cross_file, "w") as out_file:

>>> +        out_file.write(output)

>>

>> I like using string.Template instead of custom logic, but shouldn't we

>> keep the atomic rename logic? If someone hits control-C or the

>> extraction process crashes during the write, we don't want a corrupt

>> file due to a partial write.

> 

> If someone control-c's during SDK unpack, the SDK isn't usable.

> 


I don't think it will break cleanly though; instead, some things will 
work and other things won't. I was trying to avoid cryptic error 
messages such as meson hitting a syntax error. However, I agree that 
this should be addressed globally. Separate from this change, maybe we 
should drop a semaphore at the end of extraction and check it in the env 
setup script, so we can display a nice message if something didn't complete.
-- 
_______________________________________________
Openembedded-core mailing list
Openembedded-core@lists.openembedded.org
http://lists.openembedded.org/mailman/listinfo/openembedded-core
diff mbox series

Patch

diff --git a/meta/recipes-devtools/meson/meson/meson-setup.py b/meta/recipes-devtools/meson/meson/meson-setup.py
index a9749eae9d4..808e2a062f6 100755
--- a/meta/recipes-devtools/meson/meson/meson-setup.py
+++ b/meta/recipes-devtools/meson/meson/meson-setup.py
@@ -1,62 +1,31 @@ 
 #!/usr/bin/env python3
 
 import os
+import string
 import sys
 
-def bail(msg):
-    print(msg, file=sys.stderr)
-    sys.exit(1)
-
-_MARKER = '@@'
-def transform_line(line):
-    # Substitute any special markers of this form:
-    # @@ENV@@
-    # with the value of ENV, split into meson array syntax.
-    start = line.find(_MARKER)
-    if start == -1:
-        return line
-
-    end = line.rfind(_MARKER)
-    if end == start:
-        return line
-
-    # Lookup value of the env var.
-    var = line[start+len(_MARKER):end]
-    try:
-        val = os.environ[var]
-    except KeyError:
-        bail('cannot generate meson.cross; env var %s not set' % var)
+class Template(string.Template):
+    delimiter = "@"
 
-    # Transform into meson array.
-    val = ["'%s'" % x for x in val.split()]
-    val = ', '.join(val)
-    val = '[%s]' % val
+class Environ():
+    def __getitem__(self, name):
+        val = os.environ[name]
+        val = ["'%s'" % x for x in val.split()]
+        val = ', '.join(val)
+        val = '[%s]' % val
+        return val
 
-    before = line[:start]
-    after = line[end+len(_MARKER):]
-
-    return '%s%s%s' % (before, val, after)
-
-# Make sure this is really an SDK extraction environment.
 try:
     sysroot = os.environ['OECORE_NATIVE_SYSROOT']
 except KeyError:
-    bail('OECORE_NATIVE_SYSROOT env var must be set')
-
-cross_file = os.path.join(sysroot, 'usr/share/meson/meson.cross')
-tmp_cross_file = '%s.tmp' % cross_file
+    print("Not in environment setup, bailing")
+    sys.exit(1)
 
-# Read through and transform the current meson.cross.
-lines = []
-with open(cross_file, 'r') as f:
-    for line in f:
-        lines.append(transform_line(line))
+template_file = os.path.join(sysroot, 'usr/share/meson/meson.cross.template')
+cross_file = os.path.join(sysroot, 'usr/share/meson/%smeson.cross' % os.environ["TARGET_PREFIX"])
 
-# Write the transformed result to a tmp file and atomically rename it. In case
-# we crash during the file write, we don't want an invalid meson.cross file.
-with open(tmp_cross_file, 'w') as f:
-    for line in lines:
-        f.write(line)
-    f.flush()
-    os.fdatasync(f.fileno())
-os.rename(tmp_cross_file, cross_file)
+with open(template_file) as in_file:
+    template = in_file.read()
+    output = Template(template).substitute(Environ())
+    with open(cross_file, "w") as out_file:
+        out_file.write(output)
diff --git a/meta/recipes-devtools/meson/meson/meson-wrapper b/meta/recipes-devtools/meson/meson/meson-wrapper
index b2e00da513f..d4ffe60f9a1 100755
--- a/meta/recipes-devtools/meson/meson/meson-wrapper
+++ b/meta/recipes-devtools/meson/meson/meson-wrapper
@@ -10,5 +10,5 @@  fi
 unset CC CXX CPP LD AR NM STRIP
 
 exec "$OECORE_NATIVE_SYSROOT/usr/bin/meson.real" \
-     --cross-file "$OECORE_NATIVE_SYSROOT/usr/share/meson/meson.cross" \
+     --cross-file "${OECORE_NATIVE_SYSROOT}/usr/share/meson/${TARGET_PREFIX}meson.cross" \
      "$@"
diff --git a/meta/recipes-devtools/meson/nativesdk-meson_0.49.0.bb b/meta/recipes-devtools/meson/nativesdk-meson_0.49.0.bb
index 721ee8c8f12..55c57775e06 100644
--- a/meta/recipes-devtools/meson/nativesdk-meson_0.49.0.bb
+++ b/meta/recipes-devtools/meson/nativesdk-meson_0.49.0.bb
@@ -5,9 +5,6 @@  inherit nativesdk
 SRC_URI += "file://meson-setup.py \
             file://meson-wrapper"
 
-def meson_array(var, d):
-    return "', '".join(d.getVar(var).split()).join(("'", "'"))
-
 # both are required but not used by meson
 MESON_SDK_ENDIAN = "bogus-endian"
 MESON_TARGET_ENDIAN = "bogus-endian"
@@ -17,32 +14,31 @@  MESON_C_ARGS = "${MESON_TOOLCHAIN_ARGS} ${BUILDSDK_CFLAGS}"
 MESON_CPP_ARGS = "${MESON_TOOLCHAIN_ARGS} ${BUILDSDK_CXXFLAGS}"
 MESON_LINK_ARGS = "${MESON_TOOLCHAIN_ARGS} ${BUILDSDK_LDFLAGS}"
 
-# This logic is similar but not identical to that in meson.bbclass, since it's
-# generating for an SDK rather than a cross-compile. Important differences are:
+# The cross file logic is similar but not identical to that in meson.bbclass,
+# since it's generating for an SDK rather than a cross-compile. Important
+# differences are:
 # - We can't set vars like CC, CXX, etc. yet because they will be filled in with
 #   real paths by meson-setup.sh when the SDK is extracted.
 # - Some overrides aren't needed, since the SDK injects paths that take care of
 #   them.
-addtask write_config before do_install
-do_write_config[vardeps] += "MESON_C_ARGS MESON_CPP_ARGS MESON_LINK_ARGS CC CXX LD AR NM STRIP READELF"
-do_write_config() {
-    # This needs to be Py to split the args into single-element lists
-    cat >${WORKDIR}/meson.cross <<EOF
+do_install_append() {
+    install -d ${D}${datadir}/meson
+    cat >${D}${datadir}/meson/meson.cross.template <<EOF
 [binaries]
-c = @@CC@@
-cpp = @@CXX@@
-ar = @@AR@@
-nm = @@NM@@
-ld = @@LD@@
-strip = @@STRIP@@
+c = @CC
+cpp = @CXX
+ar = @AR
+nm = @NM
+ld = @LD
+strip = @STRIP
 pkgconfig = 'pkg-config'
 
 [properties]
 needs_exe_wrapper = true
-c_args = @@CFLAGS@@
-c_link_args = @@LDFLAGS@@
-cpp_args = @@CPPFLAGS@@
-cpp_link_args = @@LDFLAGS@@
+c_args = @CFLAGS
+c_link_args = @LDFLAGS
+cpp_args = @CPPFLAGS
+cpp_link_args = @LDFLAGS
 
 [host_machine]
 system = '${SDK_OS}'
@@ -50,11 +46,6 @@  cpu_family = '${SDK_ARCH}'
 cpu = '${SDK_ARCH}'
 endian = '${MESON_SDK_ENDIAN}'
 EOF
-}
-
-do_install_append() {
-    install -d ${D}${datadir}/meson
-    install -m 0644 ${WORKDIR}/meson.cross ${D}${datadir}/meson/
 
     install -d ${D}${SDKPATHNATIVE}/post-relocate-setup.d
     install -m 0755 ${WORKDIR}/meson-setup.py ${D}${SDKPATHNATIVE}/post-relocate-setup.d/