summary refs log tree commit diff
path: root/pkgs/applications/science/misc/openmodelica/mkderivation/default.nix
blob: 94029fead48ef161c39569c70a497cbe952c34f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# mkOpenModelicaDerivation is an mkDerivation function for packages
# from OpenModelica suite.

{ stdenv, lib, fetchgit, autoconf, automake, libtool, cmake, autoreconfHook, symlinkJoin }:
pkg:
let
  inherit (builtins) hasAttr getAttr length elemAt;
  inherit (lib) attrByPath concatStringsSep;


  # A few helpers functions:

  # getAttrDef is just a getAttr with default fallback
  getAttrDef = attr: default: x: attrByPath [ attr ] default x;

  # getAttr-like helper for optional append to string:
  # "Hello" + appendByAttr "a" " " {a = "world";} = "Hello world"
  # "Hello" + appendByAttr "a" " " {} = "Hello"
  appendByAttr = attr: sep: x: if hasAttr attr x then sep + (getAttr attr x) else "";

  # Are there any OM dependencies at all?
  ifDeps = length pkg.omdeps != 0;

  # Dependencies of current OpenModelica-target joined in one file tree.
  # Return the dep itself in case it is a single one.
  joinedDeps =
    if length pkg.omdeps == 1
    then elemAt pkg.omdeps 0
    else
      symlinkJoin {
        name = pkg.pname + "-omhome";
        paths = pkg.omdeps;
      };

  # Should we run ./configure for the target pkg?
  omautoconf = getAttrDef "omautoconf" false pkg;

  # Name of the make target
  omtarget = getAttrDef "omtarget" pkg.pname pkg;

  # Directory of target sources
  omdir = getAttrDef "omdir" pkg.pname pkg;

  # Simple to to m4 configuration scripts
  postPatch = lib.optionalString ifDeps ''
    sed -i ''$(find -name omhome.m4) -e 's|if test ! -z "$USINGPRESETBUILDDIR"|if test ! -z "$USINGPRESETBUILDDIR" -a -z "$OMHOME"|'
  '' +
  appendByAttr "postPatch" "\n" pkg;

  # Update shebangs in the scripts before running configuration.
  preAutoreconf = "patchShebangs --build common" +
    appendByAttr "preAutoreconf" "\n" pkg;

  # Tell OpenModelica where built dependencies are located.
  configureFlags = lib.optional ifDeps "--with-openmodelicahome=${joinedDeps}" ++
    getAttrDef "configureFlags" [ ] pkg;

  # Our own configurePhase that accounts for omautoconf
  configurePhase = ''
    runHook preConfigure
    export configureFlags="''${configureFlags} --with-ombuilddir=$PWD/build --prefix=$prefix"
    ./configure --no-recursion $configureFlags
    ${lib.optionalString omautoconf "(cd ${omdir}; ./configure $configureFlags)"}
    runHook postConfigure
  '';

  # Targets that we want to build ourselves:
  deptargets = lib.forEach pkg.omdeps (dep: dep.omtarget);

  # ... so we ask openmodelica makefile to skip those targets.
  preBuild = ''
    for target in ${concatStringsSep " " deptargets}; do
      touch ''${target}.skip;
    done
  '' +
  appendByAttr "preBuild" "\n" pkg;

  makeFlags = "${omtarget}" +
    appendByAttr "makeFlags" " " pkg;

  installFlags = "-i " +
    appendByAttr "installFlags" " " pkg;


in
stdenv.mkDerivation (pkg // {
  inherit omtarget postPatch preAutoreconf configureFlags configurePhase preBuild makeFlags installFlags;

  src = fetchgit (import ./src-main.nix);
  version = "1.17.0";

  nativeBuildInputs = getAttrDef "nativeBuildInputs" [ ] pkg
    ++ [ autoconf automake libtool cmake autoreconfHook ];

  buildInputs = getAttrDef "buildInputs" [ ] pkg
    ++ lib.optional ifDeps joinedDeps;

  dontUseCmakeConfigure = true;

  hardeningDisable = [ "format" ];
})