about summary refs log tree commit diff
path: root/pkgs/development/tools/phantomjs2/default.nix
blob: 88fd2b559848cdc266d1b871f52994c830fe117a (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
{ stdenv, fetchgit,
  bison2, flex, fontconfig, freetype, gperf, icu, openssl, libjpeg, libpng, perl, python, ruby, sqlite,
  darwin, writeScriptBin, cups
}:

let
  fakeXcrun = writeScriptBin "xcrun" ''
    #!${stdenv.shell}
    echo >&2 "Fake xcrun: ''$@"
    args=()
    while (("$#")); do
      case "$1" in
        -sdk*) shift;;
        -find*) shift;;
        *) args+=("$1");;
      esac
      shift
    done

    if [ "''${#args[@]}" -gt "0" ]; then
      echo >&2 "Fake xcrun: ''${args[@]}"
      exec "''${args[@]}"
    fi
  '';
  fakeClang = writeScriptBin "clang" ''
    #!${stdenv.shell}
    if [[ "$@" == *.c ]]; then
      exec "${stdenv.cc}/bin/clang" "$@"
    else
      exec "${stdenv.cc}/bin/clang++" "$@"
    fi
  '';

in stdenv.mkDerivation rec {
  name = "phantomjs-${version}";
  version = "2.1.1";

  # needs git submodules, so can't use fetchFromGitHub
  src = fetchgit {
    rev = "refs/tags/${version}";
    url = "https://github.com/ariya/phantomjs.git";
    sha256 = "1gyc8qxn8v4vm4lgd9njrprz46fg1j5ziq0mm888q8ms0p7jy2pi";
  };

  buildInputs = [ bison2 flex fontconfig freetype gperf icu openssl libjpeg libpng perl python ruby sqlite ]
    ++ stdenv.lib.optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [
      AGL ApplicationServices AppKit Cocoa OpenGL
      darwin.libobjc fakeClang cups
    ]);


  patchPhase = ''
    patchShebangs .
    sed -i -e 's|/bin/pwd|pwd|' src/qt/qtbase/configure
    touch src/qt/{qtbase,qtwebkit,3rdparty}/.git
  '' + stdenv.lib.optionalString stdenv.isDarwin ''
    sed -i 's,-licucore,/usr/lib/libicucore.dylib,' src/qt/qtwebkit/Source/WTF/WTF.pri
    substituteInPlace src/qt/qtwebkit/Tools/qmake/mkspecs/features/features.pri \
      --replace "ENABLE_3D_RENDERING=1" "ENABLE_3D_RENDERING=0"
    sed -i 88d src/qt/qtwebkit/Tools/qmake/mkspecs/features/features.prf
    echo 'CONFIG -= create_cmake' >> src/qt/qtwebkit/Source/api.pri
    echo 'CONFIG -= create_cmake' >> src/qt/qtwebkit/Source/widgetsapi.pri
    pushd src/qt

      substituteInPlace qtbase/configure \
        --replace /usr/bin/xcode-select true \
        --replace '/usr/bin/xcodebuild -sdk $sdk -version Path 2>/dev/null' 'echo /var/empty' \
        --replace '/usr/bin/xcrun -sdk $sdk -find' 'type -P'
      substituteInPlace qtbase/mkspecs/features/mac/default_pre.prf \
        --replace '/usr/bin/xcode-select --print-path 2>/dev/null' "echo ${stdenv.libc}" \
        --replace '/usr/bin/xcrun -find xcrun 2>/dev/null' 'echo success' \
        --replace '/usr/bin/xcodebuild -version' 'echo Xcode 7.2; echo Build version 7C68' \
        --replace 'sdk rez' ""
      for file in $(grep -rl /usr/bin/xcrun .); do
        substituteInPlace "$file" --replace "/usr/bin/xcrun" ${fakeXcrun}/bin/xcrun
      done
      substituteInPlace qtbase/src/tools/qlalr/lalr.cpp --replace _Nullable Nullable

    popd
  '';

  __impureHostDeps = stdenv.lib.optional stdenv.isDarwin "/usr/lib/libicucore.dylib";

  buildPhase = "./build.py --confirm -j$NIX_BUILD_CORES";

  enableParallelBuilding = true;

  installPhase = ''
    mkdir -p $out/share/doc/phantomjs
    cp -a bin $out
    cp -a ChangeLog examples LICENSE.BSD README.md third-party.txt $out/share/doc/phantomjs
  '' + stdenv.lib.optionalString stdenv.isDarwin ''
    install_name_tool -change \
        ${darwin.CF}/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation \
        /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation \
      -change \
        ${darwin.configd}/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration \
        /System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration \
    $out/bin/phantomjs
  '';

  meta = with stdenv.lib; {
    description = "Headless WebKit with JavaScript API";
    longDescription = ''
      PhantomJS2 is a headless WebKit with JavaScript API.
      It has fast and native support for various web standards:
      DOM handling, CSS selector, JSON, Canvas, and SVG.

      PhantomJS is an optimal solution for:
      - Headless Website Testing
      - Screen Capture
      - Page Automation
      - Network Monitoring
    '';

    homepage = http://phantomjs.org/;
    license = licenses.bsd3;

    maintainers = [ maintainers.aflatter ];
    platforms = platforms.darwin ++ platforms.linux;
  };
}