Skip to content

Commit d4e5cdb

Browse files
UebelAndrelalten
andauthored
Added module extension for fetching CPAN dependencies
Co-authored-by: Laurenz <[email protected]>
1 parent cae79dd commit d4e5cdb

File tree

13 files changed

+1267
-16
lines changed

13 files changed

+1267
-16
lines changed

.bazelci/presubmit.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ tasks:
1717
test_targets:
1818
- "//..."
1919

20+
cpan_compile_ubuntu2204:
21+
name: CPAN Compile
22+
platform: ubuntu2204
23+
run_targets:
24+
# Two compilations are performed to ensure the
25+
# results of the first are usable by the second.
26+
- "//perl/cpan/3rdparty:compiler"
27+
- "//perl/cpan/3rdparty:compiler"
28+
29+
cpan_compile_macos:
30+
name: CPAN Compile
31+
platform: macos_arm64
32+
run_targets:
33+
# Two compilations are performed to ensure the
34+
# results of the first are usable by the second.
35+
- "//perl/cpan/3rdparty:compiler"
36+
- "//perl/cpan/3rdparty:compiler"
37+
2038
e2e_ubuntu2204:
2139
platform: ubuntu2204
2240
shell_commands:

MODULE.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ register_toolchains(
3030
"@rules_perl//perl:perl_windows_x86_64_toolchain",
3131
)
3232

33+
cpan = use_extension("@rules_perl//perl/cpan:extensions.bzl", "cpan")
34+
cpan.install(
35+
name = "cpan_compiler_deps",
36+
lock = "//perl/cpan/3rdparty:cpanfile.snapshot.lock.json",
37+
)
38+
use_repo(
39+
cpan,
40+
"cpan_compiler_deps",
41+
)
42+
3343
dev_repos = use_extension("@rules_perl//perl:extensions.bzl", "perl_dev_repositories", dev_dependency = True)
3444
use_repo(
3545
dev_repos,

README.md

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,47 @@ This repository provides a hermetic [Strawberry Perl](https://strawberryperl.com
4949

5050
## Using Perl Modules
5151

52-
This is the first stab at getting a more mature set of Perl rules for Bazel. Currently, it is a manual process and, hopefully, it will be a map for automation later on.
52+
Perl modules from [CPAN](https://www.cpan.org/) can be generated using the `cpan_compiler` rule in
53+
conjunction with the `cpan` module extension.
5354

5455
### Current Steps
5556

56-
* Manually download the module that you want to use.
57-
* Add the actual files that you need to your repository.
58-
* Highly recommended that you place the files in the directory structure that each Perl file is unpacked into (you may need to run `perl Makefile.PL; make` to see the final paths)
59-
* Recommended to create a 'cpan' directory and place the files (in their required path) there.
60-
* Test::Mock::Simple does **NOT** follow this pattern as it is being used as a practical example - please see 'Simple Pure Perl Example' section.
61-
* Add the new module's information to the BUILD file in the root directory of all your modules.
62-
* the target in the `deps` attribute
63-
* At this time compiled files (result of XS) will be put in the `srcs` attribute
64-
* the directory where the module lives in the `env` attribute for the `PERL5LIB` variable
65-
66-
#### Dependencies
67-
68-
The process needs to be repeated for any dependencies that the module needs.
69-
70-
Eventually, this should be an automated process.
57+
1. Create a `cpanfile` per the [Carton](https://metacpan.org/pod/Carton) documentation.
58+
2. Create an empty `*.json` will need to be created for Bazel to use a lockfile (e.g. `cpanfile.snapshot.lock.json`)
59+
3. Define a `cpan_compiler` target:
60+
61+
```python
62+
load("//perl/cpan:cpan_compiler.bzl", "cpan_compiler")
63+
64+
cpan_compiler(
65+
name = "compiler",
66+
cpanfile = "cpanfile",
67+
lockfile = "cpanfile.snapshot.lock.json",
68+
visibility = ["//visibility:public"],
69+
)
70+
```
71+
72+
4. `bazel run` the new target.
73+
5. Define a new module in `MODULE.bazel` pointing to the Bazel `*.json` lock file:
74+
75+
```python
76+
cpan = use_extension("@rules_perl//perl/cpan:extensions.bzl", "cpan")
77+
cpan.install(
78+
name = "cpan",
79+
lock = "//perl/cpan/3rdparty:cpanfile.snapshot.lock.json",
80+
)
81+
use_repo(
82+
cpan,
83+
"cpan",
84+
)
85+
```
86+
87+
### Dependencies
88+
89+
Once the `cpan` module extension is defined, dependencies will be available through the name given to the module.
90+
Using the example in the steps above, dependencies can be accessed through `@cpan//...`. (e.g. `@cpan//:DateTime`).
91+
92+
Note that [`xs`](https://perldoc.perl.org/perlxs) dependencies are currently not supported by the `cpan` extension module.
7193

7294
### Simple Pure Perl Example
7395

perl/cpan/3rdparty/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("//perl/cpan:cpan_compiler.bzl", "cpan_compiler")
2+
3+
exports_files([
4+
"cpanfile",
5+
"cpanfile.snapshot",
6+
"cpanfile.snapshot.lock.json",
7+
])
8+
9+
cpan_compiler(
10+
name = "compiler",
11+
cpanfile = "cpanfile",
12+
lockfile = "cpanfile.snapshot.lock.json",
13+
visibility = ["//visibility:public"],
14+
)

perl/cpan/3rdparty/cpanfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requires 'Carton';
2+
requires 'File::Slurp';
3+
requires 'JSON::PP';

0 commit comments

Comments
 (0)