summaryrefslogtreecommitdiff
path: root/trivionomicon/pkgs/ibkr-tws-api/default.nix
blob: 879c3d24d177244300537fe46931297a80aa179a (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
{
  cmake,
  lib,
  fetchzip,
  intel-decimalfp,
  pkg-config,
  protobuf,
  python3Packages,
  stdenv,
}: let
  version = "1044.01";

  src-with-protobuf = stdenv.mkDerivation {
    pname = "ibkr-tws-api-src";
    inherit version;

    src = fetchzip {
      url = "https://interactivebrokers.github.io/downloads/twsapi_macunix.${version}.zip";
      hash = "sha256-9bi2Mgp3qDHz8R2lrXEOYIeBffRKOWqKxDYecybR8Eo=";
      stripRoot = false;
    };

    nativeBuildInputs = [
      protobuf
    ];

    configurePhase = ''
      runHook preConfigure

      rm IBJts/source/{cppclient/client/protobufUnix/*,pythonclient/ibapi/protobuf/*}
      protoc \
        -IIBJts/source/proto \
        --cpp_out=IBJts/source/cppclient/client/protobufUnix \
        --python_out=IBJts/source/pythonclient/ibapi/protobuf \
        IBJts/source/proto/*.proto

      sed -i '/^import / { s/\(\<[A-Za-z0-9_]*[A-Za-z0-9]_pb2\)/ibapi.protobuf.\1/g }' IBJts/source/pythonclient/ibapi/protobuf/*.py
      touch IBJts/source/pythonclient/ibapi/protobuf/__init__.py

      runHook postConfigure
    '';

    installPhase = ''
      runHook preInstall

      cp -r . $out

      runHook postInstall
    '';
  };

  native-lib = stdenv.mkDerivation {
    pname = "ibkr-tws-api-native";
    inherit version;

    src = src-with-protobuf;
    sourceRoot = "ibkr-tws-api-src-${version}/IBJts/source/cppclient/client";

    buildInputs = [
      intel-decimalfp
      protobuf
    ];

    postPatch = ''
      sed -i 's/-std=c++11/-std=c++17/' makefile
    '';

    makeFlags = [
      "LIB_DIR=${intel-decimalfp}/lib"
      "LIB_NAME=libbid.a"
    ];

    installPhase = ''
      runHook preInstall

      mkdir -p $out/lib
      cp -v lib*.so $out/lib

      runHook postInstall
    '';
  };

  test-client = stdenv.mkDerivation {
    pname = "ibkr-tws-api-test-client";
    inherit version;

    src = src-with-protobuf;
    sourceRoot = "ibkr-tws-api-src-${version}/IBJts/samples/Cpp/TestCppClient";

    nativeBuildInputs = [
      pkg-config
    ];

    buildInputs = [
      intel-decimalfp
      native-lib
      protobuf
    ];

    postPatch = ''
      sed -i "s/-std=c++11/-std=c++17/; s@-lprotobuf@$(pkg-config --libs protobuf)@" makefile
    '';

    makeFlags = [
      "TestCppClientDynamic"
      "LIB_DIR=${intel-decimalfp}/lib"
      "LIB_NAME_SO=libbid.a"
      "SOURCE_DIR=${native-lib}/lib"
    ];

    installPhase = ''
      runHook preInstall

      mkdir -p $out/bin
      install -m755 TestCppClientDynamic $out/bin/TestCppClient

      runHook postInstall
    '';

    postFixup = ''
      patchelf --add-rpath ${native-lib}/lib $out/bin/TestCppClient
    '';

    meta = {
      mainProgram = "TestCppClient";
    };
  };

  py-client = {
    buildPythonPackage,
    protobuf,
  }:
    buildPythonPackage {
      pname = "ibapi-python";
      inherit version;

      src = src-with-protobuf;
      sourceRoot = "ibkr-tws-api-src-${version}/IBJts/source/pythonclient";

      format = "setuptools";

      propagatedBuildInputs = [
        protobuf
      ];
    };
in {
  inherit test-client;
  native = native-lib;
  ibapi-python = python3Packages.callPackage py-client {};
}