build_library_test.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/sh
  2. # Where to find the sources (only used to copy zstd.h)
  3. ZSTD_SRC_ROOT="../../lib"
  4. # Temporary compiled binary
  5. OUT_FILE="tempbin"
  6. # Optional temporary compiled WebAssembly
  7. OUT_WASM="temp.wasm"
  8. # Source files to compile using Emscripten.
  9. IN_FILES="zstd.c examples/roundtrip.c"
  10. # Emscripten build using emcc.
  11. emscripten_emcc_build() {
  12. # Compile the the same example as above
  13. CC_FLAGS="-Wall -Wextra -Wshadow -Werror -Os -g0 -flto"
  14. emcc $CC_FLAGS -s WASM=1 -I. -o $OUT_WASM $IN_FILES
  15. # Did compilation work?
  16. if [ $? -ne 0 ]; then
  17. echo "Compiling ${IN_FILES}: FAILED"
  18. exit 1
  19. fi
  20. echo "Compiling ${IN_FILES}: PASSED"
  21. rm -f $OUT_WASM
  22. }
  23. # Emscripten build using docker.
  24. emscripten_docker_build() {
  25. docker container run --rm \
  26. --volume $PWD:/code \
  27. --workdir /code \
  28. emscripten/emsdk:latest \
  29. emcc $CC_FLAGS -s WASM=1 -I. -o $OUT_WASM $IN_FILES
  30. # Did compilation work?
  31. if [ $? -ne 0 ]; then
  32. echo "Compiling ${IN_FILES} (using docker): FAILED"
  33. exit 1
  34. fi
  35. echo "Compiling ${IN_FILES} (using docker): PASSED"
  36. rm -f $OUT_WASM
  37. }
  38. # Try Emscripten build using emcc or docker.
  39. try_emscripten_build() {
  40. which emcc > /dev/null
  41. if [ $? -eq 0 ]; then
  42. emscripten_emcc_build
  43. return $?
  44. fi
  45. which docker > /dev/null
  46. if [ $? -eq 0 ]; then
  47. emscripten_docker_build
  48. return $?
  49. fi
  50. echo "(Skipping Emscripten test)"
  51. }
  52. # Amalgamate the sources
  53. ./create_single_file_library.sh
  54. # Did combining work?
  55. if [ $? -ne 0 ]; then
  56. echo "Single file library creation script: FAILED"
  57. exit 1
  58. fi
  59. echo "Single file library creation script: PASSED"
  60. # Copy the header to here (for the tests)
  61. cp "$ZSTD_SRC_ROOT/zstd.h" zstd.h
  62. # Compile the generated output
  63. cc -Wall -Wextra -Werror -Wshadow -pthread -I. -Os -g0 -o $OUT_FILE zstd.c examples/roundtrip.c
  64. # Did compilation work?
  65. if [ $? -ne 0 ]; then
  66. echo "Compiling roundtrip.c: FAILED"
  67. exit 1
  68. fi
  69. echo "Compiling roundtrip.c: PASSED"
  70. # Run then delete the compiled output
  71. ./$OUT_FILE
  72. retVal=$?
  73. rm -f $OUT_FILE
  74. # Did the test work?
  75. if [ $retVal -ne 0 ]; then
  76. echo "Running roundtrip.c: FAILED"
  77. exit 1
  78. fi
  79. echo "Running roundtrip.c: PASSED"
  80. # Try Emscripten build if emcc or docker command is available.
  81. try_emscripten_build
  82. exit 0