Hi,
Some more comments:
The shortlog says “C toolchain” but the test is for the C++ toolchain not C,
correct?
> +def check_c_toolchain(d):
> + try:
> + with NamedTemporaryFile(delete=False, suffix=".c") as c_file:
Why delete=False and then manual cleanup later? You can move the compiler
invocation to inside the with and use c_file.flush() to write the dummy code to
disk, or just pass the source to the compiler through stdin with
subprocess.run(…, input=c_code).
> + c_code = """
> + #include <stdio.h>
> + int main() {
> + printf(\"Hello, World!\\n\");
No need to escape the “ inside a “””.
As we’re explicitly testing the C++ linkage to libstdc++, would it be safer to
actually call something from libstdc++ and call BUILD_CXX instead of BUILD_CC?
> + return 0;
> + }
> + """
> + c_file.write(c_code.encode('utf-8'))
Set the mode when you open the file to text and you won’t need to encode
manually.
> + c_file_name = c_file.name
> +
> + build_cc = d.getVar('BUILD_CC').strip()
BUILD_CC could include whitespace, so you should use shlex.split() to turn the
command into a list of tokens if you want to pass a list to run().
> + output_binary = c_file_name + ".out"
> + compile_command = [build_cc, c_file_name, '-o',
> output_binary,'-lstdc++']
> + result = subprocess.run(compile_command, stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
> +
> + if result.returncode == 0:
> + return None
> + else:
> + return f"C toolchain check failed to link against libstdc++.
> Please ensure libstdc++ and headers are installed.
> Error:\n{result.stderr.decode()}"
> + except Exception as e:
> + return f"An unexpected issue occurred during the C toolchain check:
> {str(e)}"
> + finally:
> + if c_file_name and os.path.exists(c_file_name):
> + os.remove(c_file_name)
> + if output_binary and os.path.exists(output_binary):
> + os.remove(output_binary)
There’s a neater way of doing this by reading source code from stdin and
writing to /dev/null, so there’s no cleanup to do. Something like this entirely
untested code:
c_code = “…”
cmd = shlex.split(d.getVar(“BUILD_CXX”)) + [“-“, “-o”, “/dev/null”, “-lstdc++”])
try:
subprocess.run(cmd, input=c_code, capture_output=True, text=True, check=True)
return None
except subprocess.CalledProcessError:
return f"An unexpected issue occurred during the C toolchain check: {str(e)}”
Cheers,
Ross
-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#210097):
https://lists.openembedded.org/g/openembedded-core/message/210097
Mute This Topic: https://lists.openembedded.org/mt/110721101/21656
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub
[[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-