From 44e1c045caaca6251c53e39adea05fbe9be8bb44 Mon Sep 17 00:00:00 2001 From: Shoichi Kaji Date: Tue, 15 Apr 2025 00:36:56 +0900 Subject: [PATCH] use Module::Build --- Build.PL | 52 ++++++++++++++++++++++++++++-- META.json | 12 +++++-- cpm.yml | 11 ------- dist.pl | 21 ++++++++++-- inc/MyBuilder.pm | 80 ++++++++++++++++++++++++++++++++++++++++++++++ planner/planner.pl | 30 ----------------- 6 files changed, 157 insertions(+), 49 deletions(-) delete mode 100644 cpm.yml create mode 100644 inc/MyBuilder.pm delete mode 100644 planner/planner.pl diff --git a/Build.PL b/Build.PL index 70f48e0..8c6a64e 100644 --- a/Build.PL +++ b/Build.PL @@ -1,2 +1,50 @@ -use Dist::Build; -Build_PL(\@ARGV, \%ENV); + +# This file was automatically generated by Dist::Zilla::Plugin::ModuleBuild v6.032. +use strict; +use warnings; + +use Module::Build 0.28; +use lib qw{inc}; use MyBuilder; + +my %module_build_args = ( + "build_requires" => { + "Module::Build" => "0.28" + }, + "configure_requires" => { + "IPC::Run3" => 0, + "Module::Build" => "0.4205", + "perl" => "v5.20.0" + }, + "dist_abstract" => "call clonefile system call", + "dist_author" => [ + "Shoichi Kaji " + ], + "dist_name" => "File-Copy-clonefile", + "dist_version" => "v0.0.9", + "license" => "perl", + "module_name" => "File::Copy::clonefile", + "recursive_test_files" => 1, + "requires" => { + "perl" => "v5.20.0" + }, + "test_requires" => { + "Test::LeakTrace" => 0 + } +); + + +my %fallback_build_requires = ( + "Module::Build" => "0.28", + "Test::LeakTrace" => 0 +); + + +unless ( eval { Module::Build->VERSION(0.4004) } ) { + delete $module_build_args{test_requires}; + $module_build_args{build_requires} = \%fallback_build_requires; +} + +my $build = MyBuilder->new(%module_build_args); + + +$build->create_build_script; diff --git a/META.json b/META.json index 9d2a1a9..15155c1 100644 --- a/META.json +++ b/META.json @@ -14,9 +14,15 @@ }, "name" : "File-Copy-clonefile", "prereqs" : { + "build" : { + "requires" : { + "Module::Build" : "0.28" + } + }, "configure" : { "requires" : { - "Dist::Build" : "0.015", + "IPC::Run3" : "0", + "Module::Build" : "0.4205", "perl" : "v5.20.0" } }, @@ -53,8 +59,8 @@ "x_contributors" : [ "Leon Timmermans " ], - "x_generated_by_perl" : "v5.40.0", - "x_serialization_backend" : "Cpanel::JSON::XS version 4.38", + "x_generated_by_perl" : "v5.40.2", + "x_serialization_backend" : "Cpanel::JSON::XS version 4.39", "x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later" } diff --git a/cpm.yml b/cpm.yml deleted file mode 100644 index c55380e..0000000 --- a/cpm.yml +++ /dev/null @@ -1,11 +0,0 @@ -prereqs: - configure: - requires: - Dist::Build: { version: '0.015' } - perl: { version: 'v5.20' } - runtime: - requires: - perl: { version: 'v5.20' } - test: - requires: - Test::LeakTrace: diff --git a/dist.pl b/dist.pl index 016c28c..6e795da 100644 --- a/dist.pl +++ b/dist.pl @@ -1,9 +1,24 @@ +my @prereq = ( + [ Prereqs => 'ConfigureRequires' ] => [ + 'IPC::Run3' => '0', + 'Module::Build' => '0.4205', + 'perl' => 'v5.20', + ], + [ Prereqs => 'RuntimeRequires' ] => [ + 'perl' => 'v5.20', + ], + [ Prereqs => 'TestRequires' ] => [ + 'Test::LeakTrace' => '0', + ], +); + my @config = ( name => 'File-Copy-clonefile', [ - 'Git::GatherDir' => [ exclude_filename => 'META.json', exclude_filename => 'LICENSE' ], - 'CopyFilesFromBuild' => [ copy => 'META.json', copy => 'LICENSE' ], + @prereq, + 'Git::GatherDir' => [ exclude_filename => 'META.json', exclude_filename => 'LICENSE', exclude_filename => 'Build.PL' ], + 'CopyFilesFromBuild' => [ copy => 'META.json', copy => 'LICENSE', copy => 'Build.PL' ], 'VersionFromMainModule' => [], 'LicenseFromModule' => [ override_author => 1 ], 'ReversionOnRelease' => [ prompt => 1 ], @@ -14,7 +29,7 @@ 'MetaProvides::Package' => [ inherit_version => 0, inherit_missing => 0 ], 'PruneFiles' => [ filename => 'dist.pl', filename => 'cpm.yml', filename => 'README.md', match => '^(xt|author|maint|example|eg)/' ], 'GitHubREADME::Badge' => [ badges => 'github_actions/test.yml' ], - 'Prereqs::From::cpmfile' => [], + 'ModuleBuild' => [ mb_class => 'MyBuilder' ], 'MetaJSON' => [], 'Git::Contributors' => [], 'License' => [], diff --git a/inc/MyBuilder.pm b/inc/MyBuilder.pm new file mode 100644 index 0000000..f72e478 --- /dev/null +++ b/inc/MyBuilder.pm @@ -0,0 +1,80 @@ +package MyBuilder; +use v5.20; +use warnings; +use experimental 'signatures'; + +package MyCBuilder { + use parent 'ExtUtils::CBuilder'; + use IPC::Run3 (); + + sub do_system ($self, @cmd) { + if (!$self->{quiet}) { + my $full = join ' ', map $self->quote_literal($_), @cmd; + print $full . "\n"; + } + IPC::Run3::run3( \@cmd, undef, \my $out, \my $err, { return_if_system_error => 1 } ); + my $exit = $?; + if (!$self->{quiet}) { + print {\*STDOUT} $out if $out; + print {\*STDERR} $err if $err; + } + return $exit == 0; + } +} + +use parent 'Module::Build'; +use ExtUtils::Constant (); +use File::Spec; +use File::Temp (); + +my $_clonefile = <<'EOF'; +#include +#include +int main() { + (void)clonefile("", "", 0); + return 0; +} +EOF + +sub new ($class, %argv) { + my $self = $class->SUPER::new( + %argv, + needs_compiler => 1, + -d ".git" ? ( extra_compiler_flags => ['-Wall', '-Wextra', '-Werror'] ) : (), + ); + $self->_try_compile_run($_clonefile) + or die "This module only supports platforms that have clonefile system call, such as macos.\n"; + $self->_write_constants; + $self; +} + +sub _try_compile_run ($self, $source) { + my $cbuilder = MyCBuilder->new(config => $self->config, quiet => !$self->verbose); + my $tempdir = File::Temp->newdir; + + my $c_file = File::Spec->catfile($tempdir, "try_compile_run.c"); + { open my $fh, ">", $c_file or die; print {$fh} $source; } + my $obj_file = eval { $cbuilder->compile(source => $c_file) }; + if ($@) { + $self->log_verbose($@); + return; + } + my $exe_file = eval { $cbuilder->link_executable(objects => [$obj_file]) }; + if ($@) { + $self->log_verbose($@); + return; + } + return $cbuilder->do_system($exe_file); +} + +sub _write_constants ($self) { + ExtUtils::Constant::WriteConstants( + NAME => $self->module_name, + NAMES => [qw(CLONE_NOFOLLOW CLONE_NOOWNERCOPY CLONE_ACL)], + PROXYSUBS => { autoload => 1 }, + C_FILE => 'lib/File/Copy/const-c.inc', + XS_FILE => 'lib/File/Copy/const-xs.inc', + ); +} + +1; diff --git a/planner/planner.pl b/planner/planner.pl deleted file mode 100644 index fb8aabc..0000000 --- a/planner/planner.pl +++ /dev/null @@ -1,30 +0,0 @@ -use v5.20; -use warnings; - -load_module("ExtUtils::Builder::Conf"); - -my $source = <<'EOF'; -#include -#include -int main() { - (void)clonefile("", "", 0); - return 0; -} -EOF - -assert_compile_run( - source => $source, - quiet => 1, - diag => 'This module only supports platforms that have clonefile system call, such as macos.', -); - -load_module('Dist::Build::XS'); -load_module('Dist::Build::XS::WriteConstants'); - -add_xs( - write_constants => { - NAMES => [qw(CLONE_NOFOLLOW CLONE_NOOWNERCOPY CLONE_ACL)], - PROXYSUBS => { autoload => 1 }, - }, - -d ".git" ? ( extra_compiler_flags => ['-Wall', '-Wextra', '-Werror'] ) : (), -);