Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using tsdl in toplevel on Windows #96

Open
jonahbeckford opened this issue Feb 16, 2024 · 20 comments
Open

Using tsdl in toplevel on Windows #96

jonahbeckford opened this issue Feb 16, 2024 · 20 comments

Comments

@jonahbeckford
Copy link
Contributor

(I'll submit a PR)

Using dune utop in an opam switch that has tsdl on Windows results in:

Fatal error: exception Dl.DL_error("dlsym: no such symbol: \"SDL_GetError\"")

That is because <opamswitch>\lib\tsdl\tsdl.cma has:

Extra C object files:
Extra C options: -L<opamswitch>/bin/../lib
Extra dynamically-loaded libraries: -ltsdl

It should be something like:

Extra C object files: SDL2.lib
Extra C options: -L<opamswitch>/bin/../lib
Extra dynamically-loaded libraries: dlltsdl.dll
@jonahbeckford
Copy link
Contributor Author

More context:

msys64\usr\bin\bash -lc 'PATH="$PWD/.ci/sd4/opamrun:$PATH"; opamrun exec -- pkg-config --libs sdl2'
Running: opam exec -- pkg-config --libs sdl2
-LY:/source/dksdk-coder/.ci/o/dkml/bin/../lib SDL2.lib

but myocamlbuild.ml only queries for:

  • libs-only-l
  • libs-only-L

which misses SDL2.lib

@dbuenzli
Copy link
Owner

Since 116ab1f tsdl has now a proper stubs library on every platform, on macOS this looks like that:

> ocamlobjinfo tsdl.cma | grep 'Extra'
Extra C object files: -lSDL2
Extra C options: -L/opt/homebrew/lib
Extra dynamically-loaded libraries: -ltsdl_stubs

in ec9ddc9 I adapted the myocambuild.ml with what I think is needed from #98.

Could you perhaps test this ?

@jonahbeckford
Copy link
Contributor Author

Could you perhaps test this ?

Give me some time to get re-acquainted and a few other things, but yes I'll test this.

@jonahbeckford
Copy link
Contributor Author

With the latest tsdl:

$ git rev-parse master
fd77252b640a835bd58c73929142f182b8544bc6

I get an error from opam install . (with some slight hacking¹ for the older Windows ctypes) that I don't remember seeing before:

Fatal error: Creation of import library not supported for this toolchain

The logs:

WARNING: `with-dkml "ocaml"` is deprecated. Use `dk Ml.Use -- "ocaml"` instead.
pkg.ml: [WARNING] Watermarks: opam fields of opam: topkg: no such command. Did you install topkg-care ?
pkg.ml: [WARNING] file opam: opam field "maintainer" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "authors" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "homepage" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "bug-reports" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "doc" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "license" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "dev-repo" undefined or unsupported
 ocamlfind ocamlopt unix.cmxa -I 'Y:\source\tsdl\_opam\lib\ocamlbuild' 'Y:\source\tsdl\_opam\lib\ocamlbuild/ocamlbuildlib.cmxa' -linkpkg myocamlbuild.ml 'Y:\source\tsdl\_opam\lib\ocamlbuild/ocamlbuild.cmx' -o myocamlbuild.exe
 ocamlfind ocamlc -g -thread -ccopt -DHAS_SDL2 -ccopt -IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include -ccopt -IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include/SDL2 -c src/tsdl_stubs.c
 mv tsdl_stubs.obj src/tsdl_stubs.obj
 ocamlfind ocamlmklib -o src/tsdl_stubs -g -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib src/tsdl_stubs.obj
 ocamlfind ocamldep -modules support/consts.ml > support/consts.ml.depends
 ocamlfind ocamlc -c -g -bin-annot -safe-string -thread -I support -I src -o support/consts.cmo support/consts.ml
 ocamlfind ocamlc -g -thread -ccopt -DHAS_SDL2 -ccopt -IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include -ccopt -IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include/SDL2 -c support/consts_stub.c
 mv consts_stub.obj support/consts_stub.obj
 ocamlfind ocamlc -linkpkg -g -custom -thread -I support support/consts.cmo support/consts_stub.obj -o support/consts.byte
 support/consts.byte src/tsdl_consts.ml
 ocamlfind ocamldep -modules src/tsdl_consts.ml > src/tsdl_consts.ml.depends
 ocamlfind ocamlc -c -g -bin-annot -safe-string -thread -I src -o src/tsdl_consts.cmo src/tsdl_consts.ml
 ocamlfind ocamldep -package 'ctypes ctypes.foreign' -modules src/tsdl.ml > src/tsdl.ml.depends
 ocamlfind ocamldep -package 'ctypes ctypes.foreign' -modules src/tsdl.mli > src/tsdl.mli.depends
 ocamlfind ocamlc -c -g -bin-annot -safe-string -thread -package 'ctypes ctypes.foreign' -I src -o src/tsdl.cmi src/tsdl.mli
 ocamlfind ocamlopt -c -g -bin-annot -safe-string -thread -I src -o src/tsdl_consts.cmx src/tsdl_consts.ml
 ocamlfind ocamlopt -c -g -bin-annot -safe-string -thread -package 'ctypes ctypes.foreign' -I src -o src/tsdl.cmx src/tsdl.ml
 ocamlfind ocamlopt -a -thread -ccopt -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib -cclib dlltsdl_stubs.dll -package 'ctypes ctypes.foreign' -I src src/tsdl_consts.cmx src/tsdl.cmx -o src/tsdl.cmxa
 ocamlfind ocamlopt -shared -linkall -thread src/libtsdl_stubs.lib -ccopt -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib -cclib dlltsdl_stubs.dll -package 'ctypes ctypes.foreign' -I src src/tsdl.cmxa -o src/tsdl.cmxs
+  ocamlfind ocamlopt -shared -linkall -thread src/libtsdl_stubs.lib -ccopt -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib -cclib dlltsdl_stubs.dll -package 'ctypes ctypes.foreign' -I src src/tsdl.cmxa -o src/tsdl.cmxs
** Fatal error: Creation of import library not supported for this toolchain

File "caml_startup", line 1:

Error: Error during linking (exit code 2)

Command exited with code 2.
pkg.ml: [ERROR] cmd ["ocamlbuild" "-use-ocamlfind" "-classic-display" "-j" "4" "-tag" "debug"
     "-build-dir" "_build" "opam" "pkg/META" "CHANGES.md" "LICENSE.md"
     "README.md" "src/tsdl.lib" "src/tsdl.cmxs" "src/tsdl.cmxa"
     "src/tsdl.cma" "src/tsdl.cmx" "src/tsdl.cmi" "src/tsdl.mli"
     "src/tsdl_consts.cmx" "src/dlltsdl_stubs.dll" "src/libtsdl_stubs.lib"
     "src/top/tsdl_top.lib" "src/top/tsdl_top.cmxs" "src/top/tsdl_top.cmxa"
     "src/top/tsdl_top.cma" "src/top/tsdl_top.cmx" "src/tsdl_top_init.ml"
     "doc/index.mld" "test/min.ml" "test/minc.c"]: exited with 10

¹ Notes so I can recreate at a later date: https://gist.github.com/jonahbeckford/0b354398726e03a3bbb36e5f22efb902

@dbuenzli
Copy link
Owner

This seems to come from flexdll (with which I'm personally totally unfamiliar). Are you in a cross-compilation setting ?

I was recently on a call with Tarides people and they told me there was a problem with cross compiling where basically topkg and ocamlfind would all be fine and do the right thing but ocamlbuild was falling short because it would try to build a library with the wrong extension because of this issue.

(Various things were discussed like working around in tokpg but in the end since there are other packages that still rely on ocamlbuild it would perhaps best be fixed there).

Perhaps related ?

@jonahbeckford
Copy link
Contributor Author

jonahbeckford commented Oct 16, 2024

I don't do cross-compiling to/from Windows and non-Windows. But just in case I mixed Windows 32-bit PE modules with 64-bit, I checked both:

  • file _opam\share\dkcoder-c\bin\ffi-8.dll and file _opam\share\dkcoder-c\bin\SDL2.dll:
    ... PE32+ executable (DLL) (GUI) x86-64, for MS Windows
  • (definitive) resetting to tsdl tag v1.0.0 and then successfully compiling with opam install .

I'll open a ticket on flexdll: ocaml/flexdll#143

@nojb
Copy link

nojb commented Oct 16, 2024

Just from a brief glance at the log, I am struck by the use of -cclib dlltsdl_stubs.dll when building the .cmxa file. Normally stubs are statically linked when producing native code. Is the use of a .dll here, expected? (Somehow this may be related to the puzzling Flexlink error.)

@dbuenzli
Copy link
Owner

Mmh right. I took that line from @jonahbeckford's patch here in ec9ddc9.

All the logic is condensed here.

I guess it works on unices, i.e. you can use the stub_l value both for static and dynamic linking but that on Windows you need to split it.

The current value should be used for the -dllib argument for byte code and something else should be given to the -cclib on Windows, tsdl_stubs.lib I suppose.

dbuenzli added a commit that referenced this issue Oct 16, 2024
@dbuenzli
Copy link
Owner

Note that what I wrote here was wrong, it failed custom mode builds and was subsequently fixed by 48b49ad.

This means that the state on a unix is:

File _build/src/tsdl.cma
Force custom: no
Extra C object files: -ltsdl_stubs -lSDL2
Extra C options: -L…
Extra dynamically-loaded libraries: -ltsdl_stubs

File _build/src/tsdl.cmxa
Extra C object files: -ltsdl_stubs -lSDL2
Extra C options: -L…

Mutatis mutandis I think the state on windows should be:

File _build/src/tsdl.cma
Force custom: no
Extra C object files: tsdl_stubs.lib SDL2.lib
Extra C options: -L…
Extra dynamically-loaded libraries: dlltsdl_stubs.dll

File _build/src/tsdl.cmxa
Extra C object files: tsdl_stubs.lib SDL2.lib
Extra C options: -L…

which commit 44bce9b should bring.

@jonahbeckford
Copy link
Contributor Author

New logs:

WARNING: `with-dkml "ocaml"` is deprecated. Use `dk Ml.Use -- "ocaml"` instead.
pkg.ml: [WARNING] Watermarks: opam fields of opam: topkg: no such command. Did you install topkg-care ?
pkg.ml: [WARNING] file opam: opam field "maintainer" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "authors" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "homepage" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "bug-reports" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "doc" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "license" undefined or unsupported
pkg.ml: [WARNING] file opam: opam field "dev-repo" undefined or unsupported
 ocamlfind ocamlopt unix.cmxa -I 'Y:\source\tsdl\_opam\lib\ocamlbuild' 'Y:\source\tsdl\_opam\lib\ocamlbuild/ocamlbuildlib.cmxa' -linkpkg myocamlbuild.ml 'Y:\source\tsdl\_opam\lib\ocamlbuild/ocamlbuild.cmx' -o myocamlbuild.exe
 ocamlfind ocamlc -g -thread -ccopt -DHAS_SDL2 -ccopt -IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include -ccopt -IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include/SDL2 -c src/tsdl_stubs.c
 mv tsdl_stubs.obj src/tsdl_stubs.obj
 ocamlfind ocamlmklib -o src/tsdl_stubs -g -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib src/tsdl_stubs.obj
 ocamlfind ocamldep -modules support/consts.ml > support/consts.ml.depends
 ocamlfind ocamlc -c -g -bin-annot -safe-string -thread -I support -I src -o support/consts.cmo support/consts.ml
 ocamlfind ocamlc -g -thread -ccopt -DHAS_SDL2 -ccopt -IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include -ccopt -IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include/SDL2 -c support/consts_stub.c
 mv consts_stub.obj support/consts_stub.obj
 ocamlfind ocamlc -linkpkg -g -custom -thread -I support support/consts.cmo support/consts_stub.obj -o support/consts.byte
 support/consts.byte src/tsdl_consts.ml
 ocamlfind ocamldep -modules src/tsdl_consts.ml > src/tsdl_consts.ml.depends
 ocamlfind ocamlc -c -g -bin-annot -safe-string -thread -I src -o src/tsdl_consts.cmo src/tsdl_consts.ml
 ocamlfind ocamldep -package 'ctypes ctypes.foreign' -modules src/tsdl.ml > src/tsdl.ml.depends
 ocamlfind ocamldep -package 'ctypes ctypes.foreign' -modules src/tsdl.mli > src/tsdl.mli.depends
 ocamlfind ocamlc -c -g -bin-annot -safe-string -thread -package 'ctypes ctypes.foreign' -I src -o src/tsdl.cmi src/tsdl.mli
 ocamlfind ocamlopt -c -g -bin-annot -safe-string -thread -I src -o src/tsdl_consts.cmx src/tsdl_consts.ml
 ocamlfind ocamlopt -c -g -bin-annot -safe-string -thread -package 'ctypes ctypes.foreign' -I src -o src/tsdl.cmx src/tsdl.ml
 ocamlfind ocamlopt -a -thread -ccopt -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib -cclib tsdl_stubs.lib -package 'ctypes ctypes.foreign' -I src src/tsdl_consts.cmx src/tsdl.cmx -o src/tsdl.cmxa
 ocamlfind ocamlopt -shared -linkall -thread src/libtsdl_stubs.lib -ccopt -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib -cclib tsdl_stubs.lib -package 'ctypes ctypes.foreign' -I src src/tsdl.cmxa -o src/tsdl.cmxs
+  ocamlfind ocamlopt -shared -linkall -thread src/libtsdl_stubs.lib -ccopt -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib -cclib tsdl_stubs.lib -package 'ctypes ctypes.foreign' -I src src/tsdl.cmxa -o src/tsdl.cmxs
** Fatal error: Cannot find file "tsdl_stubs.lib"

File "caml_startup", line 1:

Error: Error during linking (exit code 2)

Command exited with code 2.
pkg.ml: [ERROR] cmd ["ocamlbuild" "-use-ocamlfind" "-classic-display" "-j" "4" "-tag" "debug"
     "-build-dir" "_build" "opam" "pkg/META" "CHANGES.md" "LICENSE.md"
     "README.md" "src/tsdl.lib" "src/tsdl.cmxs" "src/tsdl.cmxa"
     "src/tsdl.cma" "src/tsdl.cmx" "src/tsdl.cmi" "src/tsdl.mli"
     "src/tsdl_consts.cmx" "src/dlltsdl_stubs.dll" "src/libtsdl_stubs.lib"
     "src/top/tsdl_top.lib" "src/top/tsdl_top.cmxs" "src/top/tsdl_top.cmxa"
     "src/top/tsdl_top.cma" "src/top/tsdl_top.cmx" "src/tsdl_top_init.ml"
     "doc/index.mld" "test/min.ml" "test/minc.c"]: exited with 10

The -ccopt -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib for the pkg-config library is good (although I should be using non-debug libraries):

$ dir Y:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          10/14/2024  7:38 AM          10962 ffi.lib
-a---          10/14/2024  7:46 AM          12962 SDL2_imaged.lib
-a---          10/14/2024  7:42 AM         184714 SDL2d.lib
...

But the listing of the stub libraries shows we need -cclib libtsdl_stubs.lib and maybe a -ccopt -Lsrc rather than -cclib tsdl_stubs.lib:

$ dir _opam\.opam-switch\build\tsdl.1.1.0\_build\src\*.lib
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          10/16/2024  1:26 PM           1696 libtsdl_stubs.lib
-a---          10/16/2024  1:26 PM         782752 tsdl.lib

@dbuenzli
Copy link
Owner

"src/libtsdl_stubs.lib"

A yes ocamlmklib prefixes the static library with lib. I added this in 1fb1249.

maybe a -ccopt -Lsrc

That's not going to work in the context where it's going to be invoked. For compiling things the OCaml compiler should be setup for finding the .lib in the library directory. And for bytecode by having the dll installed somewhere where CAML_LD_LIBRARY_PATH lookups them ($(opam var lib)/stublibs in opam installs).

@jonahbeckford
Copy link
Contributor Author

Thanks. So that works, but now I am getting an error similar to the original symptom:

$ opam exec -- cmake -E env --modify PATH=path_list_prepend:Y:\source\tsdl\_opam\share\dkcoder-c\bin dune utop
Fatal error: cannot load shared library dlltsdl_stubs
Reason: Cannot resolve SDL_LogMessage
Exit code 0xc0000409

$ dk Ml.Use -- ldd  ..\_opam\.opam-switch\build\tsdl.1.1.0\_build\src\dlltsdl_stubs.dll
Cannot find FLEXDLL_RELOCATE
        ntdll.dll => /c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffbbdcb0000)
        KERNEL32.DLL => /c/WINDOWS/System32/KERNEL32.DLL (0x7ffbbc5a0000)
        KERNELBASE.dll => /c/WINDOWS/System32/KERNELBASE.dll (0x7ffbbb430000)
        msvcrt.dll => /c/WINDOWS/System32/msvcrt.dll (0x7ffbbbb80000)
        dlltsdl_stubs.dll => /y/source/tsdl/_opam/.opam-switch/build/tsdl.1.1.0/_build/src/dlltsdl_stubs.dll (0x5a200000)
        ucrtbase.dll => /c/Windows/System32/ucrtbase.dll (0x7ffbbb7f0000)
        vcruntime140.dll => /c/Windows/System32/vcruntime140.dll (0x7ffbaa070000)

The ldd is telling me that 9a5f889 (implicit linking) is no longer working.

What I expect to see is the SDL2d.dll or SDL2.dll being added (compiled from my patch branch of v1.0.0: https://github.com/jonahbeckford/tsdl/tree/dkcoder):

$ dk Ml.Use -- ldd Y:\source\dksdk-coder\.ci\o\two\lib\stublibs\dlltsdl.dll
        ntdll.dll => /c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffbbdcb0000)
        KERNEL32.DLL => /c/WINDOWS/System32/KERNEL32.DLL (0x7ffbbc5a0000)
        KERNELBASE.dll => /c/WINDOWS/System32/KERNELBASE.dll (0x7ffbbb430000)
        msvcrt.dll => /c/WINDOWS/System32/msvcrt.dll (0x7ffbbbb80000)
        dlltsdl.dll => /y/source/dksdk-coder/.ci/o/two/lib/stublibs/dlltsdl.dll (0x5a200000)
        ucrtbase.dll => /c/Windows/System32/ucrtbase.dll (0x7ffbbb7f0000)
        vcruntime140.dll => /c/Windows/System32/vcruntime140.dll (0x7ffbaa070000)
        SDL2d.dll => not found
        api-ms-win-crt-environment-l1-1-0.dll => /y/VS/Common7/IDE/api-ms-win-crt-environment-l1-1-0.dll (?)
        api-ms-win-crt-stdio-l1-1-0.dll => /y/VS/Common7/IDE/api-ms-win-crt-stdio-l1-1-0.dll (?)
        api-ms-win-crt-runtime-l1-1-0.dll => /y/VS/Common7/IDE/api-ms-win-crt-runtime-l1-1-0.dll (?)

I don't have a hypothesis so it will take substantial time to debug and/or bisect to figure out what is different (which I can't do this week).

@dbuenzli
Copy link
Owner

Okay thanks. Also I think that we could remove 9a5f889 and tsdl_nop now that we have a anyways the regular stub ocaml_tsdl_log_message for supporting SDL_LogMessage's variable arguments.

I think something's wrong with the SDL pkg-config file. If you look at the log you pasted above the ocamlmklib invocation is missing a reference to the SDL2.lib:

ocamlfind ocamlmklib -o src/tsdl_stubs -g -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib src/tsdl_stubs.obj

For example on macos this looks like this:

ocamlfind ocamlmklib -o src/tsdl_stubs -g -L/opt/homebrew/lib -lSDL2 src/tsdl_stubs.o

So basically this looks empty or in other words pkg-config --libs-only-l sdl2 is not returning the right thing for ocamlmklib to make its job.

dbuenzli added a commit that referenced this issue Oct 16, 2024
Because we have a regular stub to SDL_LogMessage anyways now.
@jonahbeckford
Copy link
Contributor Author

Also I think that we could remove 9a5f889 and tsdl_nop now that we have a anyways the regular stub ocaml_tsdl_log_message for supporting SDL_LogMessage's variable arguments.

That sounds right.

I think something's wrong with the SDL pkg-config file. If you look at the log you pasted above the ocamlmklib invocation is missing a reference to the SDL2.lib:

ocamlfind ocamlmklib -o src/tsdl_stubs -g -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib src/tsdl_stubs.obj

...

So basically this looks empty or in other words pkg-config --libs-only-l sdl2 is not returning the right thing for ocamlmklib to make its job.

Yes, it is empty because it is not an -l argument:

PS Y:\source\tsdl> opam exec -- pkg-config --libs-only-l sdl2

PS Y:\source\tsdl> opam exec -- pkg-config --libs sdl2       
-LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib SDL2d.lib

PS Y:\source\tsdl> opam exec -- pkg-config --libs-only-other sdl2
SDL2d.lib

@dbuenzli
Copy link
Owner

Yes, it is empty because it is not an -l argument:

That looks wrong to me. I don't think this should be interpreted that literally. Conceptually this should output how to link your library. If your C toolchain doesn't use a -l, then the appropriate rune for linking your C library should still go there.

If you read the --lib-only-other docs is for other stuff like -pthread. I need to be able to know what the name of the library is to be able to specify the -dllib option in bytecode.

(It doesn't mean that I don't think the --libs-only-other should not be consulted and likely added in a generic pkg-config support but these flags should go as -ccopt options)

@jonahbeckford
Copy link
Contributor Author

Conceptually you are right. After researching the issue more ... I think the correct thing is to use the pkg-config --msvc-syntax option:

--msvc-syntax
This option is available only on Windows. It causes pkg-config to output -l and -L flags in the form recognized by the
Microsoft Visual C++ command-line compiler, cl. Specifically, instead of -Lx:/some/path it prints /libpath:x/some/path, and
instead of -lfoo it prints foo.lib. Note that the --libs output consists of flags for the linker, and should be placed on the cl
command line after a /link switch.

and I could make sure that the libcurl.pc is supplied in the conventional -L and -l ld format.

You would need to also convert the /libpath: output back into -L for the sake of ocamlmklib and ocamlc/ocamlopt.

What do you think about doing that instead?


The precise semantics can be seen in the pkgconf test suite ...

Given https://github.com/pkgconf/pkgconf/blob/pkgconf-2.3.0/tests/lib1/fragment-escaping-1.pc the output is https://github.com/pkgconf/pkgconf/blob/a88c0d962a987c62d98ede5a738e37ec71005cbd/tests/parser.sh#L280

@dbuenzli
Copy link
Owner

Sounds good, finger crossed ! beee83f

@dbuenzli
Copy link
Owner

You would need to also convert the /libpath: output back into -L for the sake of ocamlmklib and ocamlc/ocamlopt.

Oops. Forgot to do that but I need to go.

@dbuenzli
Copy link
Owner

Done in 9efd7ca

@jonahbeckford
Copy link
Contributor Author

jonahbeckford commented Oct 25, 2024

This can wait! Using the last commit I get the _log:

### Starting build.
# Target: src/tsdl_stubs.c, tags: { bin_annot, c, compile, debug, extension:c, file:src/tsdl_stubs.c, quiet, safe_string, thread, traverse, use_sdl2 }
 ocamlfind ocamlc -g -thread -ccopt -DHAS_SDL2 -ccopt /IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include -ccopt /IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include/SDL2 -c src/tsdl_stubs.c
# Target: src/tsdl_stubs.obj, tags: {  }
 mv tsdl_stubs.obj src/tsdl_stubs.obj
# Target: src/tsdl_stubs, tags: { bin_annot, c, debug, extension:lib, file:src/libtsdl_stubs.lib, ocamlmklib, quiet, safe_string, thread, traverse, use_sdl2 }
 ocamlfind ocamlmklib -o src/tsdl_stubs -g -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib SDL2d.lib src/tsdl_stubs.obj
+  ocamlfind ocamlmklib -o src/tsdl_stubs -g -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib SDL2d.lib src/tsdl_stubs.obj
LINK : fatal error LNK1181: cannot open input file 'SDL2d.lib'

Command exited with code 2.
# Compilation unsuccessful.

which makes no sense to me since the following works:

$ dk Ml.Use -- ls -l Y:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib/SDL2d.lib
-rw-r--r-- 1 beckf beckf 184714 Oct 14 07:42 Y:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib/SDL2d.lib

$ dk Ml.Use -- dumpbin /EXPORTS Y:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib/SDL2d.lib
SDL_AddEventWatch
...
SDL_wcsstr

I'll edit this comment once I'm done investigating.

EDIT:

With the following patch so I can see the ocamlmklib steps:

commit 3b5789b69a44469756866a3b86e174dadd43cd83 (HEAD -> tsdl-toplevel)
Author: Jonah Beckford <[email protected]>
Date:   Fri Oct 25 11:42:49 2024 -0700

    Add -v to ocamlmklib

diff --git a/myocamlbuild.ml b/myocamlbuild.ml
index c686c66..f4d482e 100644
--- a/myocamlbuild.ml
+++ b/myocamlbuild.ml
@@ -52,7 +52,7 @@ let lib_with_clib ~lib ~clib ~has_lib ~src_dir ~stublib =

     flag ["c"; "compile"; use_clib] (S clib_cflags);

-    flag ["c"; "ocamlmklib"; use_clib] (S (clib_L @ clib_l));
+    flag ["c"; "ocamlmklib"; use_clib] (S (A "-v" :: clib_L @ clib_l));

     flag ["link"; "ocaml"; "library"; "byte"; record_stub_lib]
       (S (dllibs [dynamic_stub_l] @ clib_ccopts @ clib_cclibs));

I get:

 ocamlfind ocamlopt unix.cmxa -I 'Y:\source\tsdl\_opam\lib\ocamlbuild' 'Y:\source\tsdl\_opam\lib\ocamlbuild/ocamlbuildlib.cmxa' -linkpkg myocamlbuild.ml 'Y:\source\tsdl\_opam\lib\ocamlbuild/ocamlbuild.cmx' -o myocamlbuild.exe
 ocamlfind ocamlc -g -thread -ccopt -DHAS_SDL2 -ccopt /IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include -ccopt /IY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../../include/SDL2 -c src/tsdl_stubs.c
 mv tsdl_stubs.obj src/tsdl_stubs.obj
 ocamlfind ocamlmklib -o src/tsdl_stubs -g -v -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib SDL2d.lib src/tsdl_stubs.obj
+  ocamlfind ocamlmklib -o src/tsdl_stubs -g -v -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib SDL2d.lib src/tsdl_stubs.obj
+ flexlink -x64 -merge-manifest -stack 33554432 -g -o src\dlltsdl_stubs.dll SDL2d.lib src/tsdl_stubs.obj    -LY:/source/tsdl/_opam/share/dkcoder-c/debug/lib/pkgconfig/../../lib -LC:/Users/beckf/AppData/Local/Programs/DKMLNA~1/lib/ocaml\flexdll

+ link -lib -nologo -machine:AMD64 -out:src\libtsdl_stubs.lib  SDL2d.lib src/tsdl_stubs.obj

LINK : fatal error LNK1181: cannot open input file 'SDL2d.lib'

Command exited with code 2.
pkg.ml: [ERROR] cmd ["ocamlbuild" "-use-ocamlfind" "-classic-display" "-j" "4" "-tag" "debug"
     "-build-dir" "_build" "opam" "pkg/META" "CHANGES.md" "LICENSE.md"
     "README.md" "src/tsdl.lib" "src/tsdl.cmxs" "src/tsdl.cmxa"
     "src/tsdl.cma" "src/tsdl.cmx" "src/tsdl.cmi" "src/tsdl.mli"
     "src/tsdl_consts.cmx" "src/dlltsdl_stubs.dll" "src/libtsdl_stubs.lib"
     "src/top/tsdl_top.lib" "src/top/tsdl_top.cmxs" "src/top/tsdl_top.cmxa"
     "src/top/tsdl_top.cma" "src/top/tsdl_top.cmx" "src/tsdl_top_init.ml"
     "doc/index.mld" "test/min.ml" "test/minc.c"]: exited with 10

That is a flexlink bug since it doesn't pass -L down to link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants