Skip to content

Commit

Permalink
pre includes (#30)
Browse files Browse the repository at this point in the history
* added preincs attribute

* added ci-specific bazelrc
  • Loading branch information
morganwl authored Oct 30, 2024
1 parent 78d59fe commit db7f4f0
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import %workspace%/remote.bazelrc
# import %workspace%/remote.bazelrc

test --extra_toolchains @nasm_test_toolchains//:all
test --test_output=errors
1 change: 1 addition & 0 deletions .github/workflows/ci.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import %workspace%/remote.bazelrc
7 changes: 4 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ jobs:
uname -a
bazel query --output=build "@platforms//host:host"
bazel mod show_extension rules_nasm//nasm:extensions.bzl%nasm
bazel test //... --google_credentials="${{ steps.auth.outputs.credentials_file_path }}" --${{ matrix.bzlmod }}
bazel --bazelrc=${GITHUB_WORKSPACE}/.github/workflows/ci.bazelrc test //... --google_credentials="${{ steps.auth.outputs.credentials_file_path }}" --${{ matrix.bzlmod }}
- name: Smoke
working-directory: e2e/smoke
env:
USE_BAZEL_VERSION: ${{ matrix.bazel_version }}
run:
bazel test //... --google_credentials="${{ steps.auth.outputs.credentials_file_path }}" --${{ matrix.bzlmod }}
run: |
ln -s ${GITHUB_WORKSPACE}/remote.bazelrc remote.bazelrc
bazel --bazelrc=${GITHUB_WORKSPACE}/.github/workflows/ci.bazelrc test //... --google_credentials="${{ steps.auth.outputs.credentials_file_path }}" --${{ matrix.bzlmod }}
15 changes: 9 additions & 6 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Public rules definitions for nasm.
<pre>
load("@rules_nasm//nasm:defs.bzl", "nasm_binary")

nasm_binary(<a href="#nasm_binary-name">name</a>, <a href="#nasm_binary-src">src</a>, <a href="#nasm_binary-includes">includes</a>, <a href="#nasm_binary-kwargs">kwargs</a>)
nasm_binary(<a href="#nasm_binary-name">name</a>, <a href="#nasm_binary-src">src</a>, <a href="#nasm_binary-hdrs">hdrs</a>, <a href="#nasm_binary-preincs">preincs</a>, <a href="#nasm_binary-kwargs">kwargs</a>)
</pre>


Expand All @@ -21,7 +21,8 @@ nasm_binary(<a href="#nasm_binary-name">name</a>, <a href="#nasm_binary-src">src
| :------------- | :------------- | :------------- |
| <a id="nasm_binary-name"></a>name | <p align="center"> - </p> | none |
| <a id="nasm_binary-src"></a>src | <p align="center"> - </p> | none |
| <a id="nasm_binary-includes"></a>includes | <p align="center"> - </p> | `None` |
| <a id="nasm_binary-hdrs"></a>hdrs | <p align="center"> - </p> | `None` |
| <a id="nasm_binary-preincs"></a>preincs | <p align="center"> - </p> | `None` |
| <a id="nasm_binary-kwargs"></a>kwargs | <p align="center"> - </p> | none |


Expand All @@ -32,7 +33,7 @@ nasm_binary(<a href="#nasm_binary-name">name</a>, <a href="#nasm_binary-src">src
<pre>
load("@rules_nasm//nasm:defs.bzl", "nasm_library")

nasm_library(<a href="#nasm_library-name">name</a>, <a href="#nasm_library-src">src</a>, <a href="#nasm_library-includes">includes</a>, <a href="#nasm_library-kwargs">kwargs</a>)
nasm_library(<a href="#nasm_library-name">name</a>, <a href="#nasm_library-src">src</a>, <a href="#nasm_library-hdrs">hdrs</a>, <a href="#nasm_library-preincs">preincs</a>, <a href="#nasm_library-kwargs">kwargs</a>)
</pre>

Wrap nasm_library with a CC provider.
Expand All @@ -49,7 +50,8 @@ corresponding provider, and captures any additional dependencies.
| :------------- | :------------- | :------------- |
| <a id="nasm_library-name"></a>name | <p align="center"> - </p> | none |
| <a id="nasm_library-src"></a>src | <p align="center"> - </p> | none |
| <a id="nasm_library-includes"></a>includes | <p align="center"> - </p> | `None` |
| <a id="nasm_library-hdrs"></a>hdrs | <p align="center"> - </p> | `None` |
| <a id="nasm_library-preincs"></a>preincs | <p align="center"> - </p> | `None` |
| <a id="nasm_library-kwargs"></a>kwargs | <p align="center"> - </p> | none |


Expand All @@ -60,7 +62,7 @@ corresponding provider, and captures any additional dependencies.
<pre>
load("@rules_nasm//nasm:defs.bzl", "nasm_test")

nasm_test(<a href="#nasm_test-name">name</a>, <a href="#nasm_test-src">src</a>, <a href="#nasm_test-size">size</a>, <a href="#nasm_test-includes">includes</a>, <a href="#nasm_test-kwargs">kwargs</a>)
nasm_test(<a href="#nasm_test-name">name</a>, <a href="#nasm_test-src">src</a>, <a href="#nasm_test-size">size</a>, <a href="#nasm_test-hdrs">hdrs</a>, <a href="#nasm_test-preincs">preincs</a>, <a href="#nasm_test-kwargs">kwargs</a>)
</pre>

Assemble and execute a test assembly program.
Expand All @@ -73,7 +75,8 @@ Assemble and execute a test assembly program.
| <a id="nasm_test-name"></a>name | <p align="center"> - </p> | none |
| <a id="nasm_test-src"></a>src | <p align="center"> - </p> | none |
| <a id="nasm_test-size"></a>size | <p align="center"> - </p> | `None` |
| <a id="nasm_test-includes"></a>includes | <p align="center"> - </p> | `None` |
| <a id="nasm_test-hdrs"></a>hdrs | <p align="center"> - </p> | `None` |
| <a id="nasm_test-preincs"></a>preincs | <p align="center"> - </p> | `None` |
| <a id="nasm_test-kwargs"></a>kwargs | <p align="center"> - </p> | none |


2 changes: 0 additions & 2 deletions e2e/diamond/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import %workspace%/../../remote.bazelrc

build --remote_cache=https://storage.googleapis.com/rules-nasm-bazel-cache
test --remote_cache=https://storage.googleapis.com/rules-nasm-bazel-cache
1 change: 1 addition & 0 deletions e2e/diamond/remote.bazelrc
2 changes: 0 additions & 2 deletions e2e/smoke/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import %workspace%/../../remote.bazelrc

build --remote_cache=https://storage.googleapis.com/rules-nasm-bazel-cache
test --remote_cache=https://storage.googleapis.com/rules-nasm-bazel-cache
5 changes: 3 additions & 2 deletions nasm/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

load(":library.bzl", "nasm_library")

def nasm_binary(name, src, includes=None, **kwargs):
def nasm_binary(name, src, hdrs=None, preincs=None, **kwargs):
nasm_library(
name = name + "_lib",
src = src,
includes = includes,
hdrs = hdrs,
preincs = preincs,
)

native.cc_binary(
Expand Down
16 changes: 10 additions & 6 deletions nasm/private/rules/library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

"""Rules for assembling object files."""

# load("@rules_cc//cc:defs.bzl", "cc_library")
NASM_EXTENSIONS = [".asm", ".nasm", ".s", ".i"]

def _nasm_library_impl(ctx):
"""Implement nasm library object."""
src = ctx.file.src
inputs = depset([src], transitive=[depset(ctx.files.includes)])
preincs = ctx.files.preincs
inputs = depset([src] + preincs, transitive=[depset(ctx.files.hdrs)])
out = ctx.actions.declare_file(ctx.label.name + '.o')

nasm_info = ctx.toolchains["//nasm:toolchain_type"]
Expand All @@ -16,6 +17,7 @@ def _nasm_library_impl(ctx):
args.add_all(nasm_info.args)
args.add("-I", src.dirname + "/")
args.add("-o", out)
args.add_all(preincs, before_each="-p")
args.add(src)
ctx.actions.run(
mnemonic = "NasmAssemble",
Expand All @@ -31,13 +33,14 @@ def _nasm_library_impl(ctx):
_nasm_library_inner = rule(
implementation = _nasm_library_impl,
attrs = {
"src": attr.label(allow_single_file = [".asm"]),
"includes": attr.label_list(allow_files = [".asm"]),
"src": attr.label(allow_single_file = NASM_EXTENSIONS),
"hdrs": attr.label_list(allow_files = NASM_EXTENSIONS),
"preincs": attr.label_list(allow_files = NASM_EXTENSIONS),
},
toolchains = ["//nasm:toolchain_type"],
)

def nasm_library(name, src, includes = None, **kwargs):
def nasm_library(name, src, hdrs=None, preincs=None, **kwargs):
"""Wrap nasm_library with a CC provider.
Assembled object files should be usable as C compilation units.
Expand All @@ -48,7 +51,8 @@ def nasm_library(name, src, includes = None, **kwargs):
_nasm_library_inner(
name = "%s_asm"%name,
src = src,
includes = includes,
hdrs = hdrs,
preincs = preincs,
)

native.cc_library(
Expand Down
5 changes: 3 additions & 2 deletions nasm/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

load(":library.bzl", "nasm_library")

def nasm_test(name, src, size=None, includes=None, **kwargs):
def nasm_test(name, src, size=None, hdrs=None, preincs=None, **kwargs):
"""Assemble and execute a test assembly program."""

nasm_library(
name = name + "_lib",
src = src,
includes = includes,
hdrs = hdrs,
preincs = preincs,
)

native.cc_test(
Expand Down
11 changes: 10 additions & 1 deletion tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ nasm_test(
name = "includes_test",
size = "small",
src = "includes_main.asm",
includes = [
hdrs = [
"includes_inc.asm",
],
)

nasm_test(
name = "preincludes_test",
size = "small",
src = "preincs_main.asm",
preincs = [
"includes_inc.asm"
],
)
23 changes: 23 additions & 0 deletions tests/preincs_main.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; Copyright (c) 2024 Morgan Wajda-Levie.
;
; Reference a macro that is not defined in this file or any included
; file.
BITS 64

%ifidn __OUTPUT_FORMAT__, macho64
%define MAIN _main
%else
%define MAIN main
%endif

global MAIN

section .text
MAIN:
xor rax, rax
MYAD rdx, 4, 1
cmp rdx, 5
je equal
mov rax, 1
equal:
ret

0 comments on commit db7f4f0

Please sign in to comment.