Skip to content

Commit

Permalink
chore: Try to build on gh actions
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrKovalenko committed Aug 7, 2024
1 parent ba1bf66 commit e73134b
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 151 deletions.
71 changes: 13 additions & 58 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,26 @@ jobs:
matrix:
os: [ubuntu-20.04, macos-latest]
steps:
- uses: actions/setup-node@v2
with:
node-version: '18'
- uses: actions/checkout@v2.3.2

- name: Install esy
run: npm install -g esy
- uses: actions/checkout@v2.3.2

- name: Try to restore install cache
uses: actions/cache@v2.1.1
- uses: actions/setup-node@v4
with:
path: ~/.esy/source
key: source-${{ hashFiles('**/index.json') }}
node-version: "20"

- name: Install
run: esy install

- name: Print esy cache
uses: actions/github-script@v3.0.0
id: print_esy_cache
- uses: lukka/get-cmake@latest
- name: Setup anew (or from cache) vcpkg (and does not build any package)
uses: lukka/run-vcpkg@v11
with:
script: |
const path = require('path')
const scriptPath = path.resolve('.github/workflows/print_esy_cache.js')
require(scriptPath)(core)
runVcpkgInstall: true

- name: Try to restore build cache
id: deps-cache
uses: actions/cache@v2.1.1
- name: Set-up OCaml
uses: ocaml/setup-ocaml@v3
with:
path: ${{ steps.print_esy_cache.outputs.esy_cache }}
key: build-odiff-${{ matrix.os }}-${{ hashFiles('**/index.json') }}
restore-keys: build-odiff-${{ matrix.os }}

# Here we use a low-level command. In real situation you don't have to
# but it is useful in CI as it split the log in GitHub UI.
# You can see at a glance if it is your project or your deps that break.
#
# We also use --release flag to build less.
# This allow us to spot syntax/type error more quickly.
- name: Build release dependencies

if: steps.deps-cache.outputs.cache-hit != 'true'
run: esy build-dependencies --release

- name: Build project in release
run: esy build --release

# Now that our core project build let builds others deps
- name: Build dependencies
if: steps.deps-cache.outputs.cache-hit != 'true'
run: esy build-dependencies

- name: Build project
run: esy build

- name: Ensure readme is up-to-date
run: node scripts/process-readme.js verify
ocaml-compiler: 4.10.2
dune-cache: true

# Here we cleanup if we have a cache fail because we use restore-keys.
# restore-keys take the old store even on cache fail.
# So, we have deps we don't care anymore. We prune them.
- name: Clean global store
if: steps.deps-cache.outputs.cache-hit != 'true'
run: esy cleanup .
- run: opam install . --with-test
- run: opam exec -- dune build

- name: Test
run: esy test
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ _release
*.install
images/diff.png
test/test-images/_*.png

vcpkg_installed/*
_opam/
28 changes: 16 additions & 12 deletions bin/ODiffBin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,26 @@ let ignoreRegions =
\"x1:y1-x2:y2\". Multiple regions are separated with a ','."

let cmd =
const Main.main $ base $ comp $ diffPath $ threshold $ diffMask $ failOnLayout
$ diffColor $ parsableOutput $ antialiasing $ ignoreRegions $ diffLines
$ disableGcOptimizations

let info =
let man =
[
`S Manpage.s_description;
`P "$(tname) is the fastest pixel-by-pixel image comparison tool.";
`P "Supported image types: .png, .jpg, .jpeg, .bitmap";
`P "Supported image types: .png, .jpg, .jpeg, .tiff";
]
in
( const Main.main $ base $ comp $ diffPath $ threshold $ diffMask
$ failOnLayout $ diffColor $ parsableOutput $ antialiasing $ ignoreRegions
$ diffLines $ disableGcOptimizations,
Term.info "odiff" ~version:"3.0.1" ~doc:"Find difference between 2 images."
~exits:
(Term.exit_info 0 ~doc:"on image match"
:: Term.exit_info 21 ~doc:"on layout diff when --fail-on-layout"
:: Term.exit_info 22 ~doc:"on image pixel difference"
:: Term.default_error_exits)
~man )
Cmd.info "odiff" ~version:"3.0.1" ~doc:"Find difference between 2 images."
~exits:
[
Cmd.Exit.info 0 ~doc:"on image match";
Cmd.Exit.info 21 ~doc:"on layout diff when --fail-on-layout";
Cmd.Exit.info 22 ~doc:"on image pixel difference";
]
~man

let () = Term.eval cmd |> Term.exit
let cmd = Cmd.v info cmd
let () = Cmd.eval cmd |> Stdlib.exit
100 changes: 50 additions & 50 deletions bin/Print.ml
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
open Odiff.Diff
(* open Odiff.Diff *)

let printDiffResult makeParsableOutput result =
(match (result, makeParsableOutput) with
| Layout, true -> ""
| Layout, false ->
Pastel.createElement
~children:
[
(Pastel.createElement ~color:Red ~bold:true ~children:[ "Failure!" ]
() [@JSX]);
" Images have different layout.\n";
]
() [@JSX]
| Pixel (_output, diffCount, _percentage, _lines), true when diffCount == 0 ->
""
| Pixel (_output, diffCount, _percentage, _lines), false when diffCount == 0
->
Pastel.createElement
~children:
[
(Pastel.createElement ~color:Green ~bold:true
~children:[ "Success!" ] () [@JSX]);
" Images are equal.\n";
(Pastel.createElement ~dim:true
~children:[ "No diff output created." ]
() [@JSX]);
]
() [@JSX]
| Pixel (_output, diffCount, diffPercentage, stack), true
when not (Stack.is_empty stack) ->
Int.to_string diffCount ^ ";"
^ Float.to_string diffPercentage
^ ";"
^ (stack
|> Stack.fold (fun acc line -> (line |> Int.to_string) ^ "," ^ acc) "")
| Pixel (_output, diffCount, diffPercentage, _), true ->
Int.to_string diffCount ^ ";" ^ Float.to_string diffPercentage
| Pixel (_output, diffCount, diffPercentage, _lines), false ->
Pastel.createElement
~children:
[
(Pastel.createElement ~color:Red ~bold:true ~children:[ "Failure!" ]
() [@JSX]);
" Images are different.\n";
"Different pixels: ";
(Pastel.createElement ~color:Red ~bold:true
~children:[ Printf.sprintf "%i (%f%%)" diffCount diffPercentage ]
() [@JSX]);
]
() [@JSX])
|> Console.log;
(* (match (result, makeParsableOutput) with *)
(* | Layout, true -> "" *)
(* | Layout, false -> *)
(* Pastel.createElement *)
(* ~children: *)
(* [ *)
(* (Pastel.createElement ~color:Red ~bold:true ~children:[ "Failure!" ] *)
(* () [@JSX]); *)
(* " Images have different layout.\n"; *)
(* ] *)
(* () [@JSX] *)
(* | Pixel (_output, diffCount, _percentage, _lines), true when diffCount == 0 -> *)
(* "" *)
(* | Pixel (_output, diffCount, _percentage, _lines), false when diffCount == 0 *)
(* -> *)
(* Pastel.createElement *)
(* ~children: *)
(* [ *)
(* (Pastel.createElement ~color:Green ~bold:true *)
(* ~children:[ "Success!" ] () [@JSX]); *)
(* " Images are equal.\n"; *)
(* (Pastel.createElement ~dim:true *)
(* ~children:[ "No diff output created." ] *)
(* () [@JSX]); *)
(* ] *)
(* () [@JSX] *)
(* | Pixel (_output, diffCount, diffPercentage, stack), true *)
(* when not (Stack.is_empty stack) -> *)
(* Int.to_string diffCount ^ ";" *)
(* ^ Float.to_string diffPercentage *)
(* ^ ";" *)
(* ^ (stack *)
(* |> Stack.fold (fun acc line -> (line |> Int.to_string) ^ "," ^ acc) "") *)
(* | Pixel (_output, diffCount, diffPercentage, _), true -> *)
(* Int.to_string diffCount ^ ";" ^ Float.to_string diffPercentage *)
(* | Pixel (_output, diffCount, diffPercentage, _lines), false -> *)
(* Pastel.createElement *)
(* ~children: *)
(* [ *)
(* (Pastel.createElement ~color:Red ~bold:true ~children:[ "Failure!" ] *)
(* () [@JSX]); *)
(* " Images are different.\n"; *)
(* "Different pixels: "; *)
(* (Pastel.createElement ~color:Red ~bold:true *)
(* ~children:[ Printf.sprintf "%i (%f%%)" diffCount diffPercentage ] *)
(* () [@JSX]); *)
(* ] *)
(* () [@JSX]) *)
(* |> Console.log; *)
result
2 changes: 1 addition & 1 deletion bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
(package odiff)
(flags
(:standard -w -27))
(libraries console.lib pastel.lib odiff-core odiff-io cmdliner))
(libraries odiff-core odiff-io cmdliner))
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(lang dune 2.8)
(name odiff)

; Warning: The flag set for these foreign sources overrides the `:standard` set
; of flags. However the flags in this standard set are still added to the
Expand Down
Binary file added images/out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 104 additions & 25 deletions io/config/discover.ml
Original file line number Diff line number Diff line change
@@ -1,27 +1,106 @@
module C = Configurator.V1

let _ =
C.main ~name:"odiff-c-lib-package-resolver" (fun _c ->
let spng_include_path = Sys.getenv "SPNG_INCLUDE_PATH" |> String.trim in
let spng_lib_path = Sys.getenv "SPNG_LIB_PATH" |> String.trim in
let libspng = spng_lib_path ^ "/libspng_static.a" in
let jpeg_include_path = Sys.getenv "JPEG_INCLUDE_PATH" |> String.trim in
let jpeg_lib_path = Sys.getenv "JPEG_LIB_PATH" |> String.trim in
let libjpeg = jpeg_lib_path ^ "/libjpeg.a" in
let tiff_include_path = Sys.getenv "TIFF_INCLUDE_PATH" |> String.trim in
let tiff_lib_path = Sys.getenv "TIFF_LIB_PATH" |> String.trim in
let libtiff = tiff_lib_path ^ "/libtiff.a" in
let z_lib_path = Sys.getenv "Z_LIB_PATH" |> String.trim in
let zlib = z_lib_path ^ "/libz.a" in
C.Flags.write_sexp "png_write_c_flags.sexp" [ "-I" ^ spng_include_path ];
C.Flags.write_sexp "png_write_c_library_flags.sexp" [ libspng; zlib ];
C.Flags.write_sexp "png_write_flags.sexp" [ "-cclib"; libspng ];
C.Flags.write_sexp "png_c_flags.sexp" [ "-I" ^ spng_include_path ];
C.Flags.write_sexp "png_c_library_flags.sexp" [ libspng; zlib ];
C.Flags.write_sexp "png_flags.sexp" [ "-cclib"; libspng ];
C.Flags.write_sexp "jpg_c_flags.sexp" [ "-I" ^ jpeg_include_path ];
C.Flags.write_sexp "jpg_c_library_flags.sexp" [ libjpeg ];
C.Flags.write_sexp "jpg_flags.sexp" [ "-cclib"; libjpeg ];
C.Flags.write_sexp "tiff_c_flags.sexp" [ "-I" ^ tiff_include_path ];
C.Flags.write_sexp "tiff_c_library_flags.sexp" [ libtiff; zlib ];
C.Flags.write_sexp "tiff_flags.sexp" [ "-cclib"; libtiff ])
exception Pkg_Config_Resolution_Failed of string

type pkg_config_result = { cflags : string; libs : string }
type process_result = { exit_code : int; stdout : string; stderr : string }

let run_process ~env prog args =
let stdout_fn = Filename.temp_file "stdout" ".tmp" in
let stderr_fn = Filename.temp_file "stderr" ".tmp" in
let openfile f =
Unix.openfile f
[ Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC; Unix.O_SHARE_DELETE ]
0o666
in
let stdout = openfile stdout_fn in
let stderr = openfile stderr_fn in
let stdin, stdin_w = Unix.pipe () in
Unix.close stdin_w;

let pid =
match env with
| [] ->
Unix.create_process prog
(Array.of_list (prog :: args))
stdin stdout stderr
| _ ->
let env_array = Array.of_list env in
Unix.create_process_env prog
(Array.of_list (prog :: args))
env_array stdin stdout stderr
in

Unix.close stdin;
Unix.close stdout;
Unix.close stderr;

let _, status = Unix.waitpid [] pid in

let read_file filename =
let ic = open_in filename in
let n = in_channel_length ic in
let s = really_input_string ic n in
close_in ic;
s
in

let stdout_content = read_file stdout_fn in
let stderr_content = read_file stderr_fn in

Sys.remove stdout_fn;
Sys.remove stderr_fn;

let exit_code =
match status with
| Unix.WEXITED code -> code
| Unix.WSIGNALED signal ->
raise
(Pkg_Config_Resolution_Failed
(Printf.sprintf "Process killed by signal %d" signal))
| Unix.WSTOPPED signal ->
raise
(Pkg_Config_Resolution_Failed
(Printf.sprintf "Process stopped by signal %d" signal))
in

{ exit_code; stdout = stdout_content; stderr = stderr_content }

let run_pkg_config _c lib =
let pkg_config_path = Sys.getenv_opt "PKG_CONFIG_PATH" in
let env =
match pkg_config_path with
| Some path -> [ "PKG_CONFIG_PATH=" ^ path ]
| None -> []
in

let c_flags_result = run_process ~env "pkg-config" [ "--cflags"; lib ] in
let libs_result = run_process ~env "pkg-config" [ "--libs"; lib ] in

if c_flags_result.exit_code = 0 && libs_result.exit_code == 0 then
{ cflags = c_flags_result.stdout; libs = libs_result.stdout }
else
let std_errors =
String.concat "\n" [ c_flags_result.stderr; libs_result.stderr ]
in

raise (Pkg_Config_Resolution_Failed std_errors)

let () =
C.main ~name:"odiff-c-lib-packae-resolver" (fun c ->
let png_config = run_pkg_config c "libspng_static" in
let tiff_config = run_pkg_config c "libtiff-4" in
let jpeg_config = run_pkg_config c "libturbojpeg" in

C.Flags.write_sexp "png_c_flags.sexp" [ png_config.cflags ];
C.Flags.write_sexp "png_c_library_flags.sexp" [ png_config.libs ];
C.Flags.write_sexp "png_write_c_flags.sexp" [ png_config.cflags ];
C.Flags.write_sexp "png_write_c_library_flags.sexp" [ png_config.libs ];
C.Flags.write_sexp "png_write_flags.sexp" [ "-cclib"; png_config.libs ];
C.Flags.write_sexp "png_c_flags.sexp" [ png_config.cflags ];
C.Flags.write_sexp "jpg_c_flags.sexp" [ jpeg_config.cflags ];
C.Flags.write_sexp "jpg_c_library_flags.sexp" [ jpeg_config.libs ];
C.Flags.write_sexp "jpg_flags.sexp" [ "-cclib"; jpeg_config.libs ];
C.Flags.write_sexp "tiff_c_flags.sexp" [ tiff_config.cflags ];
C.Flags.write_sexp "tiff_c_library_flags.sexp" [ tiff_config.libs ];
C.Flags.write_sexp "tiff_flags.sexp" [ "-cclib"; tiff_config.libs ])
2 changes: 0 additions & 2 deletions io/config/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
(executable
(name discover)
(ocamlc_flags str.cma)
(ocamlopt_flags str.cmxa)
(libraries dune-configurator))
Loading

0 comments on commit e73134b

Please sign in to comment.