ldc-developers / ldc

The LLVM-based D Compiler.
http://wiki.dlang.org/LDC
Other
1.21k stars 261 forks source link

Improve documentation on ldc-build-runtime #4313

Open MrcSnm opened 1 year ago

MrcSnm commented 1 year ago

I'm currently trying to use that for building, but I'm not able to achieve anything for now.

https://wiki.dlang.org/Building_LDC_runtime_libraries

So, first thing I would like to know specific versions: Which CMake version is expected to have on PC? Which gcc is expected? What versions?

I'm trying to build a runtime for a specific system from Windows and for some reason, " -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32" seems to be included.

Another thing. It is failing on testCCompiler.c Isn't there some way to skip that step? Specially because I know that my C compiler works, seems pretty bad to fail on that.

Edit: I found out that I can pass CMAKE_C_COMPILER_WORKS=1 which circumvents this problem.

The GCC version I'm using is based on 10.3.0.

I'm trying to build the d runtime for usage with newlib

For a little more of context, this is how my command looks like:

ldc-build-runtime --ninja --dFlags="-mtriple=armv7a-unknown-newlib -mcpu=cortex-a9 -g -float-abi=hard --relocation-model=static -fthread-model=local-exec" BUILD_SHARED_LIBS=OFF

I still get this error:

CMake Error at CMakeLists.txt:462 (add_custom_command):
  add_custom_command Wrong syntax.  A TARGET or OUTPUT must be specified.
Call Stack (most recent call first):
  CMakeLists.txt:507 (dc)
  CMakeLists.txt:725 (compile_druntime)
  CMakeLists.txt:795 (build_runtime_variant)
  CMakeLists.txt:910 (build_runtime_variants)

CMake Error at CMakeLists.txt:462 (add_custom_command):
  add_custom_command Wrong syntax.  A TARGET or OUTPUT must be specified.
Call Stack (most recent call first):
  CMakeLists.txt:561 (dc)
  CMakeLists.txt:732 (compile_phobos2)
  CMakeLists.txt:795 (build_runtime_variant)
  CMakeLists.txt:910 (build_runtime_variants)

CMake Error at CMakeLists.txt:462 (add_custom_command):
  add_custom_command Wrong syntax.  A TARGET or OUTPUT must be specified.
Call Stack (most recent call first):
  CMakeLists.txt:507 (dc)
  CMakeLists.txt:725 (compile_druntime)
  CMakeLists.txt:806 (build_runtime_variant)
  CMakeLists.txt:910 (build_runtime_variants)

CMake Error at CMakeLists.txt:462 (add_custom_command):
  add_custom_command Wrong syntax.  A TARGET or OUTPUT must be specified.
Call Stack (most recent call first):
  CMakeLists.txt:561 (dc)
  CMakeLists.txt:732 (compile_phobos2)
  CMakeLists.txt:806 (build_runtime_variant)
  CMakeLists.txt:910 (build_runtime_variants)

CMake Error at CMakeLists.txt:414 (file):
  file RELATIVE_PATH must be passed a full path to the directory:
  C:/msys64/usr/local/ldc-build-runtime.tmp/ldc-src/runtime/druntime/src
Call Stack (most recent call first):
  CMakeLists.txt:1022 (dc)
  CMakeLists.txt:1056 (compile_testrunner)
  CMakeLists.txt:1121 (build_test_runners)
  CMakeLists.txt:1131 (build_test_runner_variant)
  CMakeLists.txt:1142 (build_test_runner_variants)

CMake Error at CMakeLists.txt:415 (get_filename_component):
  get_filename_component called with incorrect number of arguments
Call Stack (most recent call first):
  CMakeLists.txt:1022 (dc)
  CMakeLists.txt:1056 (compile_testrunner)
  CMakeLists.txt:1121 (build_test_runners)
  CMakeLists.txt:1131 (build_test_runner_variant)
  CMakeLists.txt:1142 (build_test_runner_variants)
JohanEngelen commented 1 year ago

CMake 3.25.1 is what CI uses, so that is for sure working. CMake 3.18 was also working on my machine here (>year ago).

Please add anything you find to the Wiki page. Thank you! (e.g. how to circumvent the C compiler check)

MrcSnm commented 1 year ago

CMake 3.25.1 is what CI uses, so that is for sure working. CMake 3.18 was also working on my machine here (>year ago).

Please add anything you find to the Wiki page. Thank you! (e.g. how to circumvent the C compiler check)

Yea, I'm exactly using 3.25.1 while on running inside msys2. Though my version in the main system is more recent, I'll try to rollback the version.

I'll add the command to the wiki. But now I'm confused on this error stack from cmake

MrcSnm commented 1 year ago

Do you know what is that TARGET or OUTPUT thvariable that is missing there?

JohanEngelen commented 1 year ago

What version of LDC are you using? I cannot match the CMake reported line numbers with LDC master.

MrcSnm commented 1 year ago

1.29.0

kinke commented 1 year ago

The {d,c,linker}Flags are all supposed to be separated by semicolons, not spaces (in order to allow flag values with spaces). That's shown on the Wiki page and part of the -h output.

The CMake and gcc versions aren't that important; should work with anything non-ancient.

MrcSnm commented 1 year ago

The {d,c,linker}Flags are all supposed to be separated by semicolons, not spaces (in order to allow flag values with spaces). That's shown on the Wiki page and part of the -h output.

The CMake and gcc versions aren't that important; should work with anything non-ancient.

Yea, I've noticed that and fixed, actually I tested with a single DFLAG. I gave up in trying to build the runtime on windows and tried on Linux and got a massive different output.

I think the main problem I've noticed was that on Windows, no matter what I've done, it wasn't able to find unistd.h, which made those errors there.

I have tried manually including the unistd.h on Windows by using --cFlags="-I path/to/include" but it didn't work too.

On Linux, it starts to build, but it fails saying that time_t is not found.

My tests were with:

ldc-build-runtime --dFlags="-mtriple=armv7a-none-newlibeabi" --targetSystem="UNIX;Posix" CMAKE_C_COMPILER_WORKS=1

ldc-build-runtime --dFlags="-mtriple=armv7a-none-newlibeabi" --targetSystem="Linux" CMAKE_C_COMPILER_WORKS=1

Do you have any idea how to fix that?

kinke commented 1 year ago

With that armv7a-none-newlibeabi triple, you're targeting bare-metal, not Linux. [The --targetSystem stuff is just for our CMake script.] So the compiler won't predefine linux, and then druntime most likely doesn't define a time_t.

MrcSnm commented 1 year ago

With that armv7a-none-newlibeabi triple, you're targeting bare-metal, not Linux. [The --targetSystem stuff is just for our CMake script.] So the compiler won't predefine linux, and then druntime most likely doesn't define a time_t.

So, what is the correct way to get that predefined versions compatible with what I'm trying to do? I know that newlib defines basically everything from that. Would that be considered incomplete support from the core.stdc part giving contribution opportunity?

kinke commented 1 year ago

https://github.com/ldc-developers/ldc/pull/3946 added support for a predefined CRuntime_Newlib. druntime needs extensions to work with new targets - ISAs, OSs, C runtimes - so yeah, there's definitely contribution opportunity. :) - Upstream though (DMD repo), as GDC will most likely have good use for it too.

MrcSnm commented 1 year ago

So, I'm correct to assume that newlibeabi will define CRuntime_Newlib? Ok, I can take a look into doing the newlib port. I'll need some help with it tho I'm gonna rely on you a little :D I would really like that I could work on that on windows though. It is pretty bad keeping to change my OS only for this

MrcSnm commented 1 year ago

Btw, how's the workflow for modifying and building ldc?

kinke commented 1 year ago

So, I'm correct to assume that newlibeabi will define CRuntime_Newlib?

Yep, your triple should suffice for the predefined version.

Ok, I can take a look into doing the newlib port.

Great, all the best! :)

I would really like that I could work on that on windows though.

That should be possible, with an according armv7 gcc cross-compilation toolchain, and using a slightly adapted cmdline from the Wiki page, something like:

set CC=arm-linux-gnueabihf-gcc
ldc-build-runtime --ninja --dFlags=-mtriple=armv7a-none-newlibeabi --targetSystem=Linux;UNIX CMAKE_SYSTEM_NAME=Linux CMAKE_C_COMPILER_WORKS=True BUILD_SHARED_LIBS=OFF

Btw, how's the workflow for modifying and building ldc?

On Windows: https://wiki.dlang.org/Building_and_hacking_LDC_on_Windows_using_MSVC.

MrcSnm commented 1 year ago

So, I'm correct to assume that newlibeabi will define CRuntime_Newlib?

Yep, your triple should suffice for the predefined version.

Ok, I can take a look into doing the newlib port.

Great, all the best! :)

I would really like that I could work on that on windows though.

That should be possible, with an according armv7 gcc cross-compilation toolchain, and using a slightly adapted cmdline from the Wiki page, something like:

set CC=arm-linux-gnueabihf-gcc
ldc-build-runtime --ninja --dFlags=-mtriple=armv7a-none-newlibeabi --targetSystem=Linux;UNIX CMAKE_SYSTEM_NAME=Linux CMAKE_C_COMPILER_WORKS=True BUILD_SHARED_LIBS=OFF

Btw, how's the workflow for modifying and building ldc?

On Windows: https://wiki.dlang.org/Building_and_hacking_LDC_on_Windows_using_MSVC.

Wait, I prolly meant something else: Modifying the druntime instead of ldc, I think I should not need to deal with fiddly details such as getting my own llvm and things, am I right?

MrcSnm commented 1 year ago

Ok, I'm able to modify it if I get the source from automatically downloaded and modify it. The problem is that I'm not able to modify it if I use --ldcSrcDir=ldcSourcePath which was the plan to get modifications directly from master

MrcSnm commented 1 year ago
C:\Users\Hipreme\Desktop\test\ldc_newlib\ldc2-1.31.0-beta1-windows-x64\bin\ldc-build-runtime --ninja --ldcSrcDir=../ --dFlags=-mtriple=armv7a-none-newlibeabi --targetSystem=Linux;UNIX CMAKE_SYSTEM_NAME=Linux CMAKE_C_COMPILER_WORKS=True BUILD_SHARED_LIBS=OFF
.: Creating build directory: ldc-build-runtime.tmp
.: Invoking: cmake -DLDC_EXE_FULL=C:\Users\Hipreme\Desktop\test\ldc_newlib\ldc2-1.31.0-beta1-windows-x64\bin\ldc2.exe -DDMDFE_MINOR_VERSION=101 -DDMDFE_PATCH_VERSION=2 '-DTARGET_SYSTEM=Linux;UNIX' -DD_EXTRA_FLAGS=-mtriple=armv7a-none-newlibeabi -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_C_COMPILER_WORKS=True -DBUILD_SHARED_LIBS=OFF -G Ninja C:\Users\Hipreme\Desktop\test\ldc_newlib\ldc\runtime\../runtime
CMake Warning (dev) at CMakeLists.txt:1 (project):
  cmake_minimum_required() should be called prior to this top-level project()
  call.  Please see the cmake-commands(7) manual for usage documentation of
  both commands.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- The C compiler identification is Clang 15.0.2 with GNU-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Detecting C compile features
-- Detecting C compile features - done
-- The ASM compiler identification is Clang with GNU-like command-line
-- Found assembler: C:/Program Files/LLVM/bin/clang.exe
-- -- LDC runtime configuration:
-- --  - Building 32/64-bit libraries (MULTILIB): OFF
-- --  - Building shared libraries (BUILD_SHARED_LIBS): OFF
-- --  - Building LTO libraries (BUILD_LTO_LIBS): OFF
-- Looking for unistd.h
-- Looking for unistd.h - not found
CMake Error at CMakeLists.txt:458 (add_custom_command):
  add_custom_command Wrong syntax.  A TARGET or OUTPUT must be specified.
Call Stack (most recent call first):
  CMakeLists.txt:524 (dc)
  CMakeLists.txt:695 (compile_phobos2)
  CMakeLists.txt:758 (build_runtime_variant)
  CMakeLists.txt:790 (build_runtime_variants)

CMake Error at CMakeLists.txt:458 (add_custom_command):
  add_custom_command Wrong syntax.  A TARGET or OUTPUT must be specified.
Call Stack (most recent call first):
  CMakeLists.txt:524 (dc)
  CMakeLists.txt:695 (compile_phobos2)
  CMakeLists.txt:769 (build_runtime_variant)
  CMakeLists.txt:790 (build_runtime_variants)

CMake Warning at DRuntimeIntegrationTests.cmake:4 (message):
  GNU make could not be found.  Please install gmake/gnumake/make using your
  (platform) package installer to enable the druntime integration tests.
Call Stack (most recent call first):
  CMakeLists.txt:1089 (include)

That is the output I get if I try using my own ldc source.

MrcSnm commented 1 year ago

So, what I've done was: Clone my LDC fork. Do the ldc-build-runtime. Copy and paste the ldc-src from ldc-build-runtime into my fork, execute git reset --hard. Now it seems to accept it as an input for the build-runtime command

MrcSnm commented 1 year ago

Is there some way to make the compiler predefine Posix with the ldc-build-runtime using the armv7a-none-newlibeabi?

JohanEngelen commented 1 year ago

Is there some way to make the compiler predefine Posix with the ldc-build-runtime using the armv7a-none-newlibeabi?

You can use compiler flag -d-version=Posix, but note: newlib does not implement (all of) Posix. If you want e.g. armv7a-linux-newlibeabi to work, you have to modify LDC similar to these lines for uclibc: https://github.com/ldc-developers/ldc/blob/master/driver/main.cpp#L827-L828

MrcSnm commented 1 year ago

Is there some way to make the compiler predefine Posix with the ldc-build-runtime using the armv7a-none-newlibeabi?

You can use compiler flag -d-version=Posix, but note: newlib does not implement (all of) Posix. If you want e.g. armv7a-linux-newlibeabi to work, you have to modify LDC similar to these lines for uclibc: https://github.com/ldc-developers/ldc/blob/master/driver/main.cpp#L827-L828

Those are the strange tidbits which I find in the runtime/std. I find it so strange because it almost does exactly the same on everything and porting a libc header is really painful. I'm going to try with the -d-verison=Posix as building LDC doesn't work much here in my PC

MrcSnm commented 1 year ago

Unfortunately I can't use this workaround :( Error: version identifier Posix is reserved and cannot be set