summaryrefslogtreecommitdiff
path: root/pkgs/by-name/ca/casadi/package.nix
blob: 139aa6dce48d32a0d8fec9015fcb31a7e02cf6e2 (about) (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{
  #alpaqa,
  blas,
  blasfeo,
  bonmin,
  bzip2,
  cbc,
  clp,
  cmake,
  cplex,
  fatrop,
  fetchFromGitHub,
  fetchpatch,
  gurobi,
  highs,
  hpipm,
  lib,
  ipopt,
  lapack,
  llvmPackages,
  mumps,
  ninja,
  osqp,
  pkg-config,
  pythonSupport ? false,
  python3Packages,
  proxsuite,
  stdenv,
  sleqp,
  suitesparse,
  #sundials,
  superscs,
  spral,
  swig,
  tinyxml-2,
  withUnfree ? false,
}:

stdenv.mkDerivation (finalAttrs: {
  pname = "casadi";
  version = "3.7.0";

  src = fetchFromGitHub {
    owner = "casadi";
    repo = "casadi";
    rev = finalAttrs.version;
    hash = "sha256-WumXAWO65XnNQqHMqAwfj2Y+KGOVTWx95qIuyE1M9us=";
  };

  patches = [
    (fetchpatch {
      name = "fix-FindMUMPS.cmake.patch";
      url = "https://github.com/casadi/casadi/pull/3899/commits/274f4b23f73e60c5302bec0479fe1e92682b63d2.patch";
      hash = "sha256-3GWEWlN8dKLD6htpnOQLChldcT3hE09JWLeuCfAhY+4=";
    })
    # update include file path and link with clangAPINotes
    # https://github.com/casadi/casadi/issues/3969
    ./clang-19.diff
  ];

  postPatch = ''
    # fix case of hpipmConfig.cmake
    substituteInPlace CMakeLists.txt --replace-fail \
      "FATROP HPIPM" \
      "FATROP hpipm"

    # nix provide lib/clang headers in libclang, not in llvm.
    substituteInPlace casadi/interfaces/clang/CMakeLists.txt --replace-fail \
      '$'{CLANG_LLVM_LIB_DIR} \
      ${lib.getLib llvmPackages.libclang}/lib

    # help casadi find its own libs
    substituteInPlace casadi/core/casadi_os.cpp --replace-fail \
      "std::vector<std::string> search_paths;" \
      "std::vector<std::string> search_paths;
       search_paths.push_back(\"$out/lib\");"
  ''
  + lib.optionalString pythonSupport ''
    # fix including Python.h issue
    substituteInPlace swig/python/CMakeLists.txt --replace-fail \
      "add_library(_casadi MODULE \''${PYTHON_FILE})" \
      "add_library(_casadi MODULE \''${PYTHON_FILE})
       target_include_directories(_casadi SYSTEM PRIVATE
         ${python3Packages.python}/include/python3.${python3Packages.python.sourceVersion.minor})"

    # I have no clue. without this, it tries to install a non existent file.
    # maybe a run without SWIG_IMPORT is required before a run with SWIG_IMPORT.
    # but we need SWIG_IMPORT at some point for something else TODO
    substituteInPlace swig/python/CMakeLists.txt --replace-fail \
      "if (SWIG_IMPORT)" \
      "if (NOT SWIG_IMPORT)"
  ''
  + lib.optionalString stdenv.hostPlatform.isDarwin ''
    # this is only printing stuff, and is not defined on all CPU
    substituteInPlace casadi/interfaces/hpipm/hpipm_runtime.hpp --replace-fail \
      "d_print_exp_tran_mat" \
      "//d_print_exp_tran_mat"

    # fix missing symbols
    substituteInPlace cmake/FindCLANG.cmake --replace-fail \
      "clangBasic)" \
      "clangBasic clangASTMatchers clangSupport)"
  '';

  nativeBuildInputs = [
    cmake
    ninja
    pkg-config
  ];

  buildInputs = [
    #alpaqa
    blas
    blasfeo
    bzip2
    bonmin
    cbc
    clp
    fatrop
    highs
    hpipm
    ipopt
    lapack
    llvmPackages.clang
    llvmPackages.libclang
    llvmPackages.llvm
    mumps
    osqp
    proxsuite
    sleqp
    suitesparse
    #sundials
    superscs
    spral
    swig
    tinyxml-2
  ]
  ++ lib.optionals withUnfree [
    cplex
    gurobi
  ]
  ++ lib.optionals pythonSupport [
    python3Packages.numpy
    python3Packages.python
  ]
  ++ lib.optionals stdenv.hostPlatform.isDarwin [ llvmPackages.openmp ];

  cmakeFlags = [
    (lib.cmakeBool "WITH_PYTHON" pythonSupport)
    (lib.cmakeBool "WITH_PYTHON3" pythonSupport)
    # We don't mind always setting this cmake variable, it will be read only if
    # pythonSupport is enabled.
    "-DPYTHON_PREFIX=${placeholder "out"}/${python3Packages.python.sitePackages}"
    (lib.cmakeBool "WITH_JSON" false)
    (lib.cmakeBool "WITH_INSTALL_INTERNAL_HEADERS" true)
    (lib.cmakeBool "INSTALL_INTERNAL_HEADERS" true)
    (lib.cmakeBool "ENABLE_EXPORT_ALL" true)
    (lib.cmakeBool "SWIG_EXPORT" true)
    (lib.cmakeBool "SWIG_IMPORT" false)
    (lib.cmakeBool "WITH_OPENMP" true)
    (lib.cmakeBool "WITH_THREAD" true)
    (lib.cmakeBool "WITH_OPENCL" false)
    (lib.cmakeBool "WITH_BUILD_SUNDIALS" true) # ref. https://github.com/casadi/casadi/issues/2125
    (lib.cmakeBool "WITH_SUNDIALS" true)
    (lib.cmakeBool "WITH_BUILD_CSPARSE" false)
    (lib.cmakeBool "WITH_CSPARSE" true)
    (lib.cmakeBool "WITH_BLASFEO" true)
    (lib.cmakeBool "WITH_HPIPM" true)
    (lib.cmakeBool "WITH_FATROP" true)
    (lib.cmakeBool "WITH_BUILD_FATROP" false)
    (lib.cmakeBool "WITH_SUPERSCS" false) # packaging too chaotic
    (lib.cmakeBool "WITH_BUILD_OSQP" false)
    (lib.cmakeBool "WITH_OSQP" true)
    (lib.cmakeBool "WITH_PROXQP" true)
    (lib.cmakeBool "WITH_BUILD_TINYXML" false)
    (lib.cmakeBool "WITH_TINYXML" true)
    (lib.cmakeBool "WITH_BUILD_DSDP" true) # not sure where this come from
    (lib.cmakeBool "WITH_DSDP" true)
    (lib.cmakeBool "WITH_CLANG" true)
    (lib.cmakeBool "WITH_LAPACK" true)
    (lib.cmakeBool "WITH_QPOASES" true)
    (lib.cmakeBool "WITH_BLOCKSQP" true)
    (lib.cmakeBool "WITH_SLEQP" true)
    (lib.cmakeBool "WITH_IPOPT" true)
    (lib.cmakeBool "WITH_KNITRO" withUnfree)
    (lib.cmakeBool "WITH_SNOPT" withUnfree)
    (lib.cmakeBool "WITH_WORHP" withUnfree)
    (lib.cmakeBool "WITH_CPLEX" withUnfree)
    (lib.cmakeBool "WITH_GUROBI" withUnfree)
    (lib.cmakeBool "WITH_BONMIN" true)
    (lib.cmakeBool "WITH_CBC" true)
    (lib.cmakeBool "WITH_CLP" true)
    (lib.cmakeBool "WITH_MUMPS" true)
    (lib.cmakeBool "WITH_SPRAL" true)
    (lib.cmakeBool "WITH_HSL" withUnfree)
    (lib.cmakeBool "WITH_HIGHS" true)
    #(lib.cmakeBool "WITH_ALPAQA" true)  # this requires casadi...
  ];

  doCheck = true;

  meta = {
    description = "CasADi is a symbolic framework for numeric optimization implementing automatic differentiation in forward and reverse modes on sparse matrix-valued computational graphs. It supports self-contained C-code generation and interfaces state-of-the-art codes such as SUNDIALS, IPOPT etc. It can be used from C++, Python or Matlab/Octave";
    homepage = "https://github.com/casadi/casadi";
    license = lib.licenses.lgpl3Only;
    maintainers = with lib.maintainers; [ nim65s ];
    platforms = lib.platforms.all;
  };
})