Further support for .dylib files on mac by changing syntax for library

imports to `use -lfoo` instead of `use foo.so`
This commit is contained in:
Bruce Hill 2025-03-30 15:41:37 -04:00
parent 494e4ef006
commit 38d6189d43
11 changed files with 27 additions and 18 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
.build
*.o
*.so
*.dylib
*.tm.c
*.tm.h
*.tm.o

View File

@ -6,10 +6,10 @@ make this possible, there are a few tools available.
## Using C Libraries
In order to link against a compiled shared library, you can use `use libfoo.so`
to cause Tomo to add `-l:libfoo.so` to the linker flags when compiling your
final executable. You can also use `use <foo.h>` or `use ./foo.h` to cause Tomo
to insert a corresponding `#include` when compiling your code.
In order to link against a compiled shared library, you can use `use -lfoo` to
cause Tomo to add `-lfoo` to the linker flags when compiling your final
executable. You can also use `use <foo.h>` or `use ./foo.h` to cause Tomo to
insert a corresponding `#include` when compiling your code.
You can also `use ./foo.c` or `use ./foo.S` to use C or assembly source files
from inside a Tomo source file.

View File

@ -24,7 +24,7 @@ graph that looks like this:
```
For a more complicated example, imagine `foo.tm` imports `baz.tm` and both are
being compiled into a shared library, `libfoo.so`:
being compiled into a shared library, `libfoo.so` (or `libfoo.dylib` on Mac):
```
+---------------------------------------+

View File

@ -119,16 +119,16 @@ Finally, the resulting binary can be executed to actually run the program!
In Tomo, a shared library is built out of a *directory* that contains multiple
`.tm` files. Each `.tm` file in the directory (excluding those that start with
an underscore) will be compiled and linked together to produce a single
`libwhatever.so` file and `whatever.h` file that can be used by other Tomo
projects. You can build a library by running `tomo -L dirname/` or `tomo -L` in
the current directory.
`libwhatever.so` file (or `libwhatever.dylib` on Mac) and `whatever.h` file
that can be used by other Tomo projects. You can build a library by running
`tomo -L dirname/` or `tomo -L` in the current directory.
### Installing
If you additionally add the `-I` flag, Tomo will copy the entire directory
(excluding files and directories that begin with `.` such as `.git`) into
`~/.local/share/tomo/installed/` and create a symbolic link for the library's
`.so` file in `~/.local/share/tomo/lib/`.
`.so` file (or `.dylib` file on Mac) in `~/.local/share/tomo/lib/`.
### Using Shared Libraries

View File

@ -48,7 +48,7 @@ C code, which is then compiled using a C compiler of your choice.
: Compile the input file to an executable.
`-L`, `--library`
: Compile the input files to a library `.so` file and header.
: Compile the input files to a shared library file and header.
`-I`, `--install`
: Install the compiled executable or library.

View File

@ -1,7 +1,7 @@
# Functions for running system commands
use ./commands.c
use libunistring.so
use -lunistring
extern run_command:func(exe:Text, args:[Text], env:{Text,Text}, input:[Byte]?, output:&[Byte]?, error:&[Byte]? -> Int32)
extern command_by_line:func(exe:Text, args:[Text], env:{Text,Text} -> func(->Text?)?)

View File

@ -1,5 +1,5 @@
# Raylib wrapper for some functions and structs
use libraylib.so
use -lraylib
use <raylib.h>
use <raymath.h>

View File

@ -1,6 +1,6 @@
# A simple HTTP library built using CURL
use libcurl.so
use -lcurl
use <curl/curl.h>
struct HTTPResponse(code:Int, body:Text)

View File

@ -61,14 +61,22 @@ func main(paths:[Path]):
curl @curl_flags @url | tar xz -C ~/.local/share/tomo/installed --strip-components=1 --one-top-level=@hash
echo @original_url > ~/.local/share/tomo/installed/@hash/source.url
tomo -L ~/.local/share/tomo/installed/@hash
if [ "`uname -s`" = "Darwin" ]; then
ln -f -s ../installed/@hash/lib@hash.dylib ~/.local/share/tomo/lib/lib@hash.dylib
else
ln -f -s ../installed/@hash/lib@hash.so ~/.local/share/tomo/lib/lib@hash.so
fi
`:get_output()!)
if alias:
say($Shell(
set -exuo pipefail
ln -f -s @hash ~/.local/share/tomo/installed/@alias
if [ "`uname -s`" = "Darwin" ]; then
ln -f -s lib@hash.dylib ~/.local/share/tomo/lib/lib@alias.dylib
else
ln -f -s lib@hash.so ~/.local/share/tomo/lib/lib@alias.so
fi
):get_output()!)
say("$\[1]Installed $url!$\[]")

View File

@ -2487,14 +2487,14 @@ PARSER(parse_use) {
int what;
if (name[0] == '<' || ends_with(name, ".h")) {
what = USE_HEADER;
} else if (starts_with(name, "-l")) {
what = USE_SHARED_OBJECT;
} else if (ends_with(name, ".c")) {
what = USE_C_CODE;
} else if (ends_with(name, ".S") || ends_with(name, ".s")) {
what = USE_ASM;
} else if (starts_with(name, "./") || starts_with(name, "/") || starts_with(name, "../") || starts_with(name, "~/")) {
what = USE_LOCAL;
} else if (ends_with(name, ".so")) {
what = USE_SHARED_OBJECT;
} else {
what = USE_MODULE;

View File

@ -625,7 +625,7 @@ void build_file_dependency_graph(Path_t path, Table_t *to_compile, Table_t *to_l
break;
}
case USE_SHARED_OBJECT: {
Text_t lib = Text$format("-l:%s", use->path);
Text_t lib = Text$from_str(use->path);
Table$set(to_link, &lib, ((Bool_t[1]){1}), Table$info(&Text$info, &Bool$info));
break;
}