Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ $(eval $(call MAKE_VAR_CACHE_FOR,RUNTIME_BITCODE_RELEASE_URL))
LLVM_PATH?=$(BUILD)/llvm-static
LLVM_CONFIG?=$(LLVM_PATH)/bin/llvm-config

# Specify where to download the fswatch release source tarball from.
# This needs to get bumped explicitly here to update to a different version.
FSWATCH_RELEASE_URL?=https://github.com/emcrisostomo/fswatch/releases/download/1.16.0/fswatch-1.16.0.tar.gz
$(eval $(call MAKE_VAR_CACHE_FOR,FSWATCH_RELEASE_URL))

# Determine which flavor of the C++ standard library to link against.
# We choose libstdc++ on Linux, and libc++ on FreeBSD and MacOS.
ifneq (,$(findstring linux,$(TARGET_PLATFORM)))
Expand Down Expand Up @@ -175,6 +180,21 @@ endif
# including the LLVM extensions C++ file we need to build and link.
CRYSTAL_PATH?=$(shell env $(shell crystal env) printenv CRYSTAL_PATH | rev | cut -d ':' -f 1 | rev)

# Download the fswatch library sourece code to build it.
$(BUILD)/fswatch: $(MAKE_VAR_CACHE)/FSWATCH_RELEASE_URL
rm -rf $@-tmp
mkdir -p $@-tmp
cd $@-tmp && curl -L --fail -sS "${FSWATCH_RELEASE_URL}" \
| tar --strip-components=1 -xzvf -
rm -rf $@
mv $@-tmp $@
touch $@

# Build the static library for the fswatch library sourece code to build it.
LIB_FSWATCH?=$(BUILD)/fswatch/libfswatch/src/libfswatch/.libs/libfswatch.a
$(LIB_FSWATCH): $(BUILD)/fswatch
cd $^ && env CFLAGS="-target $(CLANG_TARGET_PLATFORM)" ./configure && make

# Download the runtime bitcode library we have built separately.
# See github.com/savi-lang/runtime-bitcode for more info.
lib/libsavi_runtime: $(MAKE_VAR_CACHE)/RUNTIME_BITCODE_RELEASE_URL
Expand Down Expand Up @@ -267,11 +287,11 @@ $(BUILD)/savi-spec.o: spec/all.cr $(LLVM_PATH) $(shell find src lib spec -name '

# Build the Savi compiler executable, by linking the above targets together.
# This variant of the target compiles in release mode.
$(BUILD)/savi-release: $(BUILD)/savi-release.o $(BUILD)/llvm_ext.bc $(BUILD)/llvm_ext_for_savi.bc lib/libsavi_runtime
$(BUILD)/savi-release: $(BUILD)/savi-release.o $(BUILD)/llvm_ext.bc $(BUILD)/llvm_ext_for_savi.bc lib/libsavi_runtime $(LIB_FSWATCH)
mkdir -p `dirname $@`
${CLANG} -O3 -o $@ -flto=thin -fPIC \
$(BUILD)/savi-release.o $(BUILD)/llvm_ext.bc $(BUILD)/llvm_ext_for_savi.bc \
${CRYSTAL_RT_LIBS} \
${CRYSTAL_RT_LIBS} $(LIB_FSWATCH) \
-target $(CLANG_TARGET_PLATFORM) \
`sh -c 'ls $(LLVM_PATH)/lib/libclang*.a'` \
`sh -c 'ls $(LLVM_PATH)/lib/liblld*.a'` \
Expand All @@ -280,11 +300,11 @@ $(BUILD)/savi-release: $(BUILD)/savi-release.o $(BUILD)/llvm_ext.bc $(BUILD)/llv

# Build the Savi compiler executable, by linking the above targets together.
# This variant of the target compiles in debug mode.
$(BUILD)/savi-debug: $(BUILD)/savi-debug.o $(BUILD)/llvm_ext.bc $(BUILD)/llvm_ext_for_savi.bc lib/libsavi_runtime
$(BUILD)/savi-debug: $(BUILD)/savi-debug.o $(BUILD)/llvm_ext.bc $(BUILD)/llvm_ext_for_savi.bc lib/libsavi_runtime $(LIB_FSWATCH)
mkdir -p `dirname $@`
${CLANG} -O0 -o $@ -flto=thin -fPIC \
$(BUILD)/savi-debug.o $(BUILD)/llvm_ext.bc $(BUILD)/llvm_ext_for_savi.bc \
${CRYSTAL_RT_LIBS} \
${CRYSTAL_RT_LIBS} $(LIB_FSWATCH) \
-target $(CLANG_TARGET_PLATFORM) \
`sh -c 'ls $(LLVM_PATH)/lib/libclang*.a'` \
`sh -c 'ls $(LLVM_PATH)/lib/liblld*.a'` \
Expand All @@ -294,11 +314,11 @@ $(BUILD)/savi-debug: $(BUILD)/savi-debug.o $(BUILD)/llvm_ext.bc $(BUILD)/llvm_ex

# Build the Savi specs executable, by linking the above targets together.
# This variant of the target will be used when running tests.
$(BUILD)/savi-spec: $(BUILD)/savi-spec.o $(BUILD)/llvm_ext.bc $(BUILD)/llvm_ext_for_savi.bc lib/libsavi_runtime
$(BUILD)/savi-spec: $(BUILD)/savi-spec.o $(BUILD)/llvm_ext.bc $(BUILD)/llvm_ext_for_savi.bc lib/libsavi_runtime $(LIB_FSWATCH)
mkdir -p `dirname $@`
${CLANG} -O0 -o $@ -flto=thin -fPIC \
$(BUILD)/savi-spec.o $(BUILD)/llvm_ext.bc $(BUILD)/llvm_ext_for_savi.bc \
${CRYSTAL_RT_LIBS} \
${CRYSTAL_RT_LIBS} $(LIB_FSWATCH) \
-target $(CLANG_TARGET_PLATFORM) \
`sh -c 'ls $(LLVM_PATH)/lib/libclang*.a'` \
`sh -c 'ls $(LLVM_PATH)/lib/liblld*.a'` \
Expand Down
3 changes: 3 additions & 0 deletions lib/.shards.info
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ shards:
clang:
git: https://github.com/crystal-lang/clang.cr.git
version: 0.3.0
fswatch:
git: https://github.com/bcardiff/crystal-fswatch.git
version: 0.1.0+git.commit.04aca8703e22fb0ed3d7a39316a49bc0e30eefba
9 changes: 9 additions & 0 deletions lib/fswatch/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
9 changes: 9 additions & 0 deletions lib/fswatch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf

# Libraries don't need dependency lock
# Dependencies will be locked in applications that use them
/shard.lock
6 changes: 6 additions & 0 deletions lib/fswatch/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: crystal

# Uncomment the following if you'd like Travis to run specs and check code formatting
# script:
# - crystal spec
# - crystal tool format --check
21 changes: 21 additions & 0 deletions lib/fswatch/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Brian J. Cardiff

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
39 changes: 39 additions & 0 deletions lib/fswatch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# crystal-fswatch

[fswatch](https://emcrisostomo.github.io/fswatch/) bindings for [Crystal](https://crystal-lang.org/)

## Installation

1. Add the dependency to your `shard.yml`:

```yaml
dependencies:
fswatch:
github: bcardiff/crystal-fswatch
```

2. Run `shards install`

## Usage

```crystal
require "fswatch"

FSWatch.watch "." do |event|
pp! event
end

sleep 10 # keep main fiber busy to prevent exiting
```

## Contributing

1. Fork it (<https://github.com/bcardiff/crystal-fswatch/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [Brian J. Cardiff](https://github.com/bcardiff) - creator and maintainer
20 changes: 20 additions & 0 deletions lib/fswatch/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '3'

services:
ubuntu:
build:
context: ./docker
dockerfile: Dockerfile.ubuntu
volumes:
- .:/src
working_dir: /src
entrypoint: /bin/bash

alpine:
build:
context: ./docker
dockerfile: Dockerfile.alpine
volumes:
- .:/src
working_dir: /src
entrypoint: /bin/sh
16 changes: 16 additions & 0 deletions lib/fswatch/docker/Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM crystallang/crystal:0.35.1-alpine

# Based on https://github.com/emcrisostomo/fswatch/blob/master/docker/alpine/Dockerfile.in

RUN apk add --no-cache file git autoconf automake libtool gettext gettext-dev make g++ texinfo curl

ENV ROOT_HOME /root
ENV FSWATCH_BRANCH 1.14.0

WORKDIR ${ROOT_HOME}
RUN git clone https://github.com/emcrisostomo/fswatch.git

WORKDIR ${ROOT_HOME}/fswatch
RUN git checkout ${FSWATCH_BRANCH}
RUN ./autogen.sh && ./configure && make -j
RUN make install
4 changes: 4 additions & 0 deletions lib/fswatch/docker/Dockerfile.ubuntu
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM crystallang/crystal:0.35.1

RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y libfswatch-dev
1 change: 1 addition & 0 deletions lib/fswatch/lib
15 changes: 15 additions & 0 deletions lib/fswatch/playground/01_monitor_curdir.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "../src/fswatch"

session = FSWatch::Session.new
session.add_path __DIR__
session.on_change do |event|
pp! event
end

puts "Starting monitor"
session.start_monitor

sleep 10

puts "Stopping monitor"
session.stop_monitor
17 changes: 17 additions & 0 deletions lib/fswatch/playground/02_monitor_curdir.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "../src/fswatch"

# pp! FSWatch.verbose
# FSWatch.verbose = true
# pp! FSWatch.verbose

i = 5
FSWatch.watch __DIR__, latency: 3.0, recursive: false do |event|
i -= 1
pp! event
end

while i > 0
puts "Waiting for #{i} events"
sleep 0.1
Fiber.yield
end
8 changes: 8 additions & 0 deletions lib/fswatch/playground/03_monitor_top_sleep.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "../src/fswatch"

FSWatch.watch "./playground", recursive: true do |event|
pp! event, event.path, event.created?, event.is_file?
end

puts "sleeping..."
sleep 10
18 changes: 18 additions & 0 deletions lib/fswatch/scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

SCRIPT_PATH="$(realpath "$0")"
SCRIPT_ROOT="$(dirname "$SCRIPT_PATH")"

CRYSTAL_LIB=$SCRIPT_ROOT/../lib/crystal_lib/src/main.cr
OUTPUT=$SCRIPT_ROOT/../src/lib_fswatch.cr

crystal run $CRYSTAL_LIB -- "$SCRIPT_ROOT/lib_fswatch.cr" |
sed -e '/lib LibFSWatch/a\'$'\n''\ \ alias Bool = LibC::Int' |
sed 's/LibC::Bool/Bool/g' |
sed -e '/^.*enum EventFlag.*/i\'$'\n''\ \ @[Flags]' |
sed '/$ALL_EVENT_FLAGS/ s/^/#/' > $OUTPUT

crystal tool format $OUTPUT

# libfswatch uses LibC::Bool that is not in std-lib
# $ALL_EVENT_FLAGS is not a valid symbol
7 changes: 7 additions & 0 deletions lib/fswatch/scripts/lib_fswatch.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@[Include(
"/usr/local/Cellar/fswatch/1.15.0/include/libfswatch/c/libfswatch.h",
"/usr/local/Cellar/fswatch/1.15.0/include/libfswatch/c/libfswatch_types.h",
prefix: %w(FSW_ fsw_))]
@[Link("fswatch", pkg_config: "libfswatch")]
lib LibFSWatch
end
16 changes: 16 additions & 0 deletions lib/fswatch/shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: fswatch
version: 0.1.0

authors:
- Brian J. Cardiff <bcardiff@gmail.com>

crystal: 0.36.1

license: MIT

libraries:
libfswatch: ~> 1.15

development_dependencies:
crystal_lib:
github: crystal-lang/crystal_lib
Loading