# -*- mode: perl -*-
use alienfile 1.68;
use lib q{lib};
use Alien::OpenMP::configure;

configure {
  if ($^O eq 'darwin') {
    requires 'File::Which' => '1.27';
    requires 'Path::Tiny'  => '0.053';
  }
  unless (Alien::OpenMP::configure->is_known) {
    Alien::OpenMP::configure->unsupported(__PACKAGE__);
    die "Unsupported platform/compiler combination (no known OpenMP configuration)";
  }
};

meta->interpolator->replace_helper(cc => sub { $Alien::OpenMP::configure::CCNAME });

plugin 'Probe::CBuilder' => (
  lang    => 'C',
  cflags  => Alien::OpenMP::configure->cflags,
  libs    => Alien::OpenMP::configure->libs,
  options => { quiet => 0 },
  program => join("\n" => <DATA>),
);

after probe => sub {
  my $build = shift;
  $build->install_prop->{alien_openmp_compiler_has_openmp} = 1;
  $build->runtime_prop->{auto_include} = Alien::OpenMP::configure->auto_include;
  $build->runtime_prop->{version} = $Alien::OpenMP::VERSION;

  # Reuse the same shell-free preprocessor capture used for compiler-family
  # detection.  This avoids shell redirection and stdin-pipe behavior on
  # Windows while retaining the enabled OpenMP flags for _OPENMP detection.
  my $defines = Alien::OpenMP::configure->_openmp_defines;
  my @props = qw{openmp_version version};
  my $runtime = Alien::OpenMP::configure->version_from_preprocessor($defines);
  @{$build->runtime_prop}{@props} = @$runtime{@props};
};

share {
  before download => sub {
    my $build = shift;
    Alien::OpenMP::configure->unsupported($build);
    die "Unsupported platform/compiler combination; OpenMP will not be built";
  };
};

__DATA__
/* OpenMP detection test program.  Do not require a particular thread count:
 * runtime policy (OMP_DYNAMIC, OMP_THREAD_LIMIT, containers, schedulers, etc.)
 * must not make compiler/runtime detection fail. */
#include <omp.h>
#ifndef _OPENMP
# error OpenMP was not enabled by the compiler
#endif
int main(void) {
  return omp_get_max_threads() < 1;
}
