Affix
view release on metacpan or search on metacpan
t/017_affix_build.t view on Meta::CPAN
export
extern(C) int add_d(int a, int b) { return a + b; }
run_test( 'odin', 'Odin', <<~'', 'add_odin', 'odin' );
package main
@(export)
add_odin :: proc "c" (a, b: i32) -> i32 {
return a + b
}
run_test( 'fortran', 'Fortran', <<~'', 'add_f', 'gfortran' );
function add_f(a, b) bind(c, name='add_f')
use iso_c_binding
integer(c_int), value :: a, b
integer(c_int) :: add_f
add_f = a + b
end function
run_test( 'nim', 'Nim', <<~'', 'add_nim', 'nim' );
proc add_nim(a, b: cint): cint {.exportc, dynlib.} =
return a + b
run_test( 'v', 'V', <<~'', 'add_v', 'v' );
[export: 'add_v']
fn add_v(a int, b int) int {
return a + b
}
run_test( 'pascal', 'Pascal', <<~'', 'add_pas', 'fpc' );
library test_pas;
function add_pas(a, b: LongInt): LongInt; cdecl; export;
begin
add_pas := a + b;
end;
exports add_pas;
begin end.
run_test( 'cr', 'Crystal', <<~'', 'add_cr', 'crystal' );
fun add_cr(a : Int32, b : Int32) : Int32
a + b
end
run_test( 'swift', 'Swift', <<~'', 'add_swift', 'swiftc' );
@_cdecl("add_swift")
public func add_swift(a: Int32, b: Int32) -> Int32 {
return a + b
}
run_test( 'assembly', 'Assembly', $Config{archname} =~ /arm64|aarch64/ ? <<~'' : $^O eq 'MSWin32' ? <<~'': <<~'', 'add_asm', 'nasm' );
; ARM64: add w0, w0, w1
.global add_asm
.text
.align 2
add_asm:
add w0, w0, w1
ret
; Win64 x86_64: RCX + RDX -> RAX
global add_asm
section .text
add_asm:
mov eax, ecx
add eax, edx
ret
; SysV x86_64: RDI + RSI -> RAX
global add_asm
section .text
add_asm:
mov eax, edi
add eax, esi
ret
run_test( 'cobol', 'Cobol', <<~'', 'add_cob', 'cobc' );
IDENTIFICATION DIVISION.
PROGRAM-ID. add_cob.
DATA DIVISION.
LINKAGE SECTION.
01 A PIC 9(9) USAGE COMP-5.
01 B PIC 9(9) USAGE COMP-5.
01 R PIC 9(9) USAGE COMP-5.
PROCEDURE DIVISION USING A, B, R.
ADD A TO B GIVING R.
GOBACK.
END PROGRAM add_cob.
subtest 'Polyglot: Number Cruncher (C + Fortran + ASM)' => sub {
skip_all "Missing compilers" unless bin_path( $Config{cc} ) && bin_path('gfortran');
# C is our orchestrator
my $c_src = $TMP_DIR->child('math_core.c');
$c_src->spew_utf8(<<~'C');
#include <stdio.h>
#ifdef _WIN32
__declspec(dllexport)
#endif
int core_version() { return 1; }
C
# Fortran does the math
my $f_src = $TMP_DIR->child('math_algos.f90');
$f_src->spew_utf8(<<~'F90');
function fortran_add(a, b) bind(c, name='fortran_add')
use iso_c_binding
integer(c_int), value :: a, b
integer(c_int) :: fortran_add
fortran_add = a + b
end function
F90
# Assembly for optimization
my $asm_bin;
my $asm_src;
my $asm_file_name;
if ( $Config{archname} =~ /arm64|aarch64/ ) {
$asm_bin = $Config{cc};
$asm_file_name = 'fast.s';
$asm_src = <<~'' }
.global asm_inc
.text
.align 2
asm_inc:
add w0, w0, #1
ret
elsif ( $^O eq 'MSWin32' ) {
$asm_bin = 'nasm';
$asm_file_name = 'fast.asm';
$asm_src = <<~'' }
global asm_inc
section .text
asm_inc:
mov eax, ecx
inc eax
ret
else {
$asm_bin = 'nasm';
$asm_file_name = 'fast.asm';
$asm_src = <<~'' }
global asm_inc
section .text
asm_inc:
mov eax, edi
inc eax
ret
skip_all "Missing Assembler ($asm_bin)" unless bin_path($asm_bin);
my $asm_file = $TMP_DIR->child($asm_file_name);
$asm_file->spew_utf8($asm_src);
#
my $compiler = Affix::Build->new( name => 'number_cruncher', build_dir => $TMP_DIR );
$compiler->add($c_src);
$compiler->add($f_src);
$compiler->add($asm_file);
ok( lives { $compiler->link() }, 'Linked Number Cruncher' ) or note $@;
ok( $compiler->libname->exists, 'Library exists' );
enjoin( $compiler->libname, 'core_version', 'fortran_add', 'asm_inc' );
};
subtest 'Polyglot: Modern Stack (C++ + Rust + Zig)' => sub {
skip_all 'Missing compilers' unless bin_path('g++') && bin_path('rustc') && bin_path('zig');
skip_all 'Rust/MinGW target missing' unless check_rust_gnu();
# C++ for ease of ABI
my $cpp_src = $TMP_DIR->child('interface.cpp');
$cpp_src->spew_utf8(<<~'');
extern "C" {
#ifdef _WIN32
__declspec(dllexport)
#endif
int cpp_interface() { return 2025; }
}
# Rust for safety
my $rs_src = $TMP_DIR->child('safety.rs');
$rs_src->spew_utf8(<<~'');
#[no_mangle]
pub extern "C" fn rust_safe_add(a: i32, b: i32) -> i32 {
a + b
}
# Zig for logic
my $zig_src = $TMP_DIR->child('logic.zig');
$zig_src->spew_utf8(<<~'');
export fn zig_calc() i32 {
return 42;
}
#
my $compiler = Affix::Build->new( name => 'modern_stack', build_dir => $TMP_DIR );
$compiler->add($cpp_src);
$compiler->add($rs_src);
$compiler->add($zig_src);
ok( lives { $compiler->link() }, 'Linked Modern Stack' ) or note $@;
enjoin( $compiler->libname, 'cpp_interface', 'rust_safe_add', 'zig_calc' );
};
subtest 'Polyglot: Kitchen Sink (C, C++, Rust, Zig, Dlang, Fortran, ASM)' => sub {
my @reqs = qw(g++ zig dmd gfortran);
push @reqs, $Config{cc}; # System CC
push @reqs, ( $Config{archname} =~ /arm64/ ? $Config{cc} : 'nasm' );
t/017_affix_build.t view on Meta::CPAN
my $c = Affix::Build->new( name => 'mega_lib', build_dir => $TMP_DIR );
#
my $f1 = $TMP_DIR->child('f1.c');
$f1->spew_utf8(<<~'');
#ifdef _WIN32
__declspec(dllexport)
#endif
int func_c( ) { return 1; }
$c->add($f1);
#
my $f2 = $TMP_DIR->child('f2.cpp');
$f2->spew_utf8(<<~'');
extern "C" {
#ifdef _WIN32
__declspec(dllexport)
#endif
int func_cpp( ) { return 2; }
}
$c->add($f2);
#
my $f3 = $TMP_DIR->child('f3.rs');
$f3->spew_utf8(<<~'');
#[no_mangle]
pub extern "C" fn func_rs( )->i32{ 3 }
$c->add($f3);
#
my $f4 = $TMP_DIR->child('f4.zig');
$f4->spew_utf8(<<~'');
export fn func_zig() i32 { return 4; }
$c->add($f4);
#
my $f5 = $TMP_DIR->child('f5.d');
$f5->spew_utf8( ( $^O eq 'MSWin32' ? <<~'' : '' ) . <<~'' );
import core.sys.windows.dll;
mixin SimpleDllMain;
export
extern(C) int func_d() { return 5; }
$c->add($f5);
#
my $f6 = $TMP_DIR->child('f6.f90');
$f6->spew_utf8(<<~'');
function func_f() bind(c, name='func_f')
use iso_c_binding
integer(c_int) :: func_f
func_f=6
end function
$c->add($f6);
#
my $asm_ext = ( $Config{archname} =~ /arm64/ ) ? 's' : 'asm';
my $f7 = $TMP_DIR->child("f7.$asm_ext");
$f7->spew_utf8( ( $^O eq 'MSWin32' || $Config{archname} !~ /arm64/ ) ? <<~'' : <<~'' );
; x86/x64
global func_asm
section .text
func_asm:
mov eax, 7
ret
; ARM64
.global func_asm
.text
func_asm:
mov w0, #7
ret
$c->add($f7);
#
ok( lives { $c->link() }, 'Linked Kitchen Sink' ) or note $@;
#
enjoin( $c->libname, 'func_c', 'func_cpp', 'func_rs', 'func_zig', 'func_d', 'func_f', 'func_asm' );
};
#
done_testing;
( run in 0.561 second using v1.01-cache-2.11-cpan-5b529ec07f3 )