Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bb8afe8

Browse files
authoredNov 17, 2024··
Merge pull request #2 from msaelices/main
Fixes in variadic arguments. Include some new logic from mojo-stdlib-extensions. Adapted to latest Mojo nightly. Unit tests
2 parents c876e4c + 8270c5e commit bb8afe8

22 files changed

+7346
-2063
lines changed
 

‎.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# GitHub syntax highlighting
2+
pixi.lock linguist-language=YAML linguist-generated=true

‎.github/workflows/main.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Main workflow
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
test:
12+
uses: ./.github/workflows/test.yml
13+
14+
package:
15+
uses: ./.github/workflows/package.yml
16+
17+
publish:
18+
uses: ./.github/workflows/publish.yml
19+
secrets:
20+
PREFIX_API_KEY: ${{ secrets.PREFIX_API_KEY }}

‎.github/workflows/package.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Create package
2+
3+
on:
4+
workflow_call:
5+
6+
jobs:
7+
package:
8+
name: Package
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v4
13+
- name: Run the package command
14+
run: |
15+
curl -ssL https://magic.modular.com | bash
16+
source $HOME/.bash_profile
17+
magic run mojo package src/libc -o libc.mojopkg
18+
19+
- name: Upload package as artifact
20+
uses: actions/upload-artifact@v4
21+
with:
22+
name: libc-package
23+
path: libc.mojopkg

‎.github/workflows/publish.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Build and publish
2+
3+
on:
4+
workflow_call:
5+
secrets:
6+
PREFIX_API_KEY:
7+
required: true
8+
9+
jobs:
10+
publish:
11+
name: Publish package
12+
strategy:
13+
matrix:
14+
include:
15+
- { target: linux-64, os: ubuntu-latest }
16+
- { target: osx-arm64, os: macos-14 }
17+
fail-fast: false
18+
runs-on: ${{ matrix.os }}
19+
timeout-minutes: 5
20+
defaults:
21+
run:
22+
shell: bash
23+
steps:
24+
- name: Checkout repo
25+
uses: actions/checkout@v4
26+
27+
- name: Build package for target platform
28+
env:
29+
TARGET_PLATFORM: ${{ matrix.target }}
30+
PREFIX_API_KEY: ${{ secrets.PREFIX_API_KEY }}
31+
CONDA_BLD_PATH: ${{ runner.workspace }}/.rattler
32+
run: |
33+
curl -ssL https://magic.modular.com | bash
34+
source $HOME/.bash_profile
35+
36+
# Temporary method to fetch the rattler binary.
37+
RATTLER_BINARY="rattler-build-aarch64-apple-darwin"
38+
if [[ $TARGET_PLATFORM == "linux-64" ]]; then RATTLER_BINARY="rattler-build-x86_64-unknown-linux-musl"; fi
39+
curl -SL --progress-bar https://github.com/prefix-dev/rattler-build/releases/latest/download/${RATTLER_BINARY} -o rattler-build
40+
chmod +x rattler-build
41+
42+
# Build and push
43+
magic run build --target-platform=$TARGET_PLATFORM
44+
magic run publish -c mojo-community-nightly

‎.github/workflows/test.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Run the testing suite
2+
3+
on:
4+
workflow_call:
5+
6+
jobs:
7+
test:
8+
name: Run tests
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v4
13+
- name: Run the test suite
14+
run: |
15+
curl -ssL https://magic.modular.com | bash
16+
source $HOME/.bash_profile
17+
magic run mojo test -I src/ tests/

‎.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# pixi environments
2+
.pixi
3+
*.egg-info
4+
# magic environments
5+
.magic
6+
output

‎.pre-commit-config.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v2.3.0
4+
hooks:
5+
- id: check-yaml
6+
- id: end-of-file-fixer
7+
- id: trailing-whitespace
8+
- repo: local
9+
hooks:
10+
- id: mojo-format
11+
name: mojo-format
12+
entry: magic run mojo format -l 88
13+
language: system
14+
files: '\.(mojo|🔥)$'
15+
stages: [commit]

‎LICENSE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TO FIGURE IT OUT

‎README.md

+39-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
11
# Mojo's libc support
22

3-
> Note: its a work in progress. Variadic syscall not working at the moment.
3+
## Getting Started
4+
5+
The only dependency for `libc` is Mojo.
6+
7+
You can install Mojo following the instructions from the [Modular website](https://www.modular.com/max/mojo).
8+
9+
Once you have created a Mojo project using the `magic` tool,
10+
11+
1. Add the `mojo-community` channel to your `mojoproject.toml`, e.g:
12+
```toml
13+
[project]
14+
channels = ["conda-forge", "https://conda.modular.com/max", "https://repo.prefix.dev/mojo-community"]
15+
```
16+
2. Add `libc` as a dependency:
17+
```toml
18+
[dependencies]
19+
libc = ">=0.1.4"
20+
```
21+
3. Run `magic install` at the root of your project, where `mojoproject.toml` is located
22+
4. `libc` should now be installed as a dependency. You can import libc functions from the library, e.g:
23+
```mojo
24+
from libc import socket
25+
```
426
527
## Supported Functionality
28+
629
### Basic socket connections
7-
Example for [server](https://github.com/crisadamo/mojo-libc/blob/main/Libc.mojo#L1575) and [client](https://github.com/crisadamo/mojo-libc/blob/main/Libc.mojo#L1534)
830
9-
> Note: `getaddrinfo` is not working properly.
31+
See the examples in [examples/sockets/](examples/sockets/) directory.
1032
11-
To test the socket functionality there are two functions:
12-
- `__test_socket_server__` that start listening for connections on 127.0.0.1:8083 and once a client connects it send the `"Hello, Mojo!"` message.
13-
- `__test_socket_client__` that connects to 127.0.0.1:8083 send the message `"Hello, world Server"` and prints the reply from the server.
33+
### Basic file system operations
1434
15-
In order to test it you need to create two notebooks, on the first one you need to run the `__test_socket_server__()` and then on the second one run `__test_socket_client__()`.
35+
See the examples in [examples/files/](examples/files/) directory.
36+
## Building the project
1637
38+
To build the project, execute the following command:
1739
18-
### Basic file system operations
19-
Example [here](https://github.com/crisadamo/mojo-libc/blob/main/Libc.mojo#L1636)
40+
```bash
41+
./scripts/build.sh
42+
```
43+
44+
## Running the tests
45+
46+
To run the tests, execute the following command:
2047

21-
To test it you can play around with the `__test_file__` function.
48+
```bash
49+
./scripts/run-tests.sh
50+
```

‎examples/files/print.mojo

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from libc import (
2+
FD_STDOUT,
3+
c_char,
4+
c_int,
5+
c_void,
6+
printf,
7+
strlen,
8+
write,
9+
)
10+
11+
12+
fn main():
13+
var format_str = String("Hello %s\n")
14+
var format_ptr = format_str.unsafe_cstr_ptr()
15+
var name = String("world")
16+
var name_ptr = name.unsafe_cstr_ptr()
17+
_ = printf(format_ptr, name_ptr)
18+
var msg = String("Hello world, again\n")
19+
var msg_ptr = msg.unsafe_cstr_ptr()
20+
_ = write(FD_STDOUT, msg_ptr.bitcast[c_void](), len(msg))

‎examples/sockets/client.mojo

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from memory import UnsafePointer
2+
from utils import StaticTuple
3+
4+
from libc import (
5+
AF_INET,
6+
AI_PASSIVE,
7+
SOCK_STREAM,
8+
SHUT_RDWR,
9+
addrinfo,
10+
c_char,
11+
connect,
12+
getaddrinfo,
13+
htons,
14+
in_addr,
15+
sizeof,
16+
sockaddr,
17+
sockaddr_in,
18+
socket,
19+
shutdown,
20+
)
21+
22+
23+
fn get_ip_address(host: String) raises -> in_addr:
24+
var host_ptr = host.unsafe_cstr_ptr()
25+
var my_addrinfo = addrinfo()
26+
var servinfo = UnsafePointer[addrinfo]().alloc(1)
27+
servinfo.init_pointee_move(my_addrinfo)
28+
29+
var hints = addrinfo()
30+
hints.ai_family = AF_INET
31+
hints.ai_socktype = SOCK_STREAM
32+
hints.ai_flags = AI_PASSIVE
33+
34+
var error = getaddrinfo(
35+
host_ptr,
36+
UnsafePointer[c_char](),
37+
UnsafePointer.address_of(hints),
38+
UnsafePointer.address_of(servinfo),
39+
)
40+
if error != 0:
41+
raise Error("getaddrinfo failed: {}".format(error))
42+
43+
var addrinfo = servinfo[]
44+
var ai_addr = addrinfo.ai_addr
45+
46+
return ai_addr.bitcast[sockaddr_in]()[].sin_addr
47+
48+
49+
fn main() raises:
50+
var ip: in_addr
51+
var host = "localhost"
52+
var port = 8080
53+
print("Connecting to ", host, " on port ", port)
54+
55+
ip = get_ip_address(host)
56+
57+
# Convert ip address to network byte order.
58+
var addr: sockaddr_in = sockaddr_in(
59+
AF_INET, htons(port), ip, StaticTuple[c_char, 8](0, 0, 0, 0, 0, 0, 0, 0)
60+
)
61+
var addr_ptr = UnsafePointer[sockaddr_in].address_of(addr).bitcast[sockaddr]()
62+
var sock = socket(AF_INET, SOCK_STREAM, 0)
63+
64+
if connect(sock, addr_ptr, sizeof[sockaddr_in]()) == -1:
65+
print("Failed to connect to server")
66+
else:
67+
print("Connected to server")
68+
69+
_ = shutdown(sock, SHUT_RDWR)

‎examples/sockets/server.mojo

Whitespace-only changes.
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.