<commit_before>CC?=clang
ERLANG_PATH:=$(shell erl -eval 'io:format("~s~n", [lists:concat([code:root_dir(), "/erts-", erlang:system_info(version), "/include"])])' -s init stop -noshell)
ERLANG_FLAGS?=-I$(ERLANG_PATH)
EBIN_DIR?=ebin

NOOUT=2>&1 >/dev/null

BUILD_DIR=_build
DEPS_DIR=deps
PRIV_DIR=priv
SRC_DIR=src
TEST_DIR=test
TMP_DIR=tmp

CMARK=cmark

CMARK_SRC_REPO=https://github.com/jgm/cmark.git
CMARK_SRC_DIR=$(TMP_DIR)/$(CMARK)
CMARK_C_SRC_DIR=$(CMARK_SRC_DIR)/$(SRC_DIR)
CMARK_C_FILES=$(sort $(filter-out %main.c %print.c, $(wildcard $(CMARK_C_SRC_DIR)/*.c)))
CMARK_H_FILES=$(sort $(wildcard $(CMARK_C_SRC_DIR)/*.h))
CMARK_INC_FILES=$(sort $(wildcard $(CMARK_C_SRC_DIR)/*.inc))
CMARK_BUILD_DIR=$(CMARK_SRC_DIR)/build

PYTHON=python3
CMARK_SPECS_REPO=https://github.com/jgm/CommonMark.git
CMARK_SPECS_DIR=$(TMP_DIR)/specs
CMARK_SPECS_FILE=$(CMARK_SPECS_DIR)/spec.txt
CMARK_SPECS_RUNNER=$(CMARK_SPECS_DIR)/test/spec_tests.py
CMARK_SPECS_JSON=$(TEST_DIR)/$(CMARK)_specs.json

C_SRC_DIR=c_src
C_SRC_C_FILES=$(sort $(wildcard $(C_SRC_DIR)/*.c))
C_SRC_O_FILES=$(C_SRC_C_FILES:.c=.o)

NIF_SRC=$(SRC_DIR)/$(CMARK)_nif.c
NIF_LIB=$(PRIV_DIR)/$(CMARK).so

OPTIONS=-shared
ifeq ($(shell uname),Darwin)
OPTIONS+= -dynamiclib -undefined dynamic_lookup
endif
INCLUDES=-I$(C_SRC_DIR)

OPTFLAGS?=-fPIC -std=c99 -Wall
CFLAGS=-O2 $(OPTFLAGS) $(INCLUDES)
CMARK_OPTFLAGS=-DNDEBUG

### TARGETS

all: version check-cc $(NIF_LIB)

all-dev: dev-prepare all

all-test: all test

all-dev-test: all-dev test

build-objects: $(C_SRC_O_FILES)

$(C_SRC_DIR)/%.o : $(C_SRC_DIR)/%.c
	$(CC) $(CMARK_OPTFLAGS) $(CFLAGS) -o $@ -c $<

$(C_SRC_DIR):
	mkdir -p $@

$(PRIV_DIR):
	@mkdir -p $@ $(NOOUT)

$(NIF_LIB): $(PRIV_DIR) $(C_SRC_O_FILES)
	$(CC) $(CFLAGS) $(ERLANG_FLAGS) $(OPTIONS) $(C_SRC_O_FILES) $(NIF_SRC) -o $@

$(CMARK):
	@mix deps.get
	@mix compile

### TEST

spec: all $(CMARK_SPECS_JSON)
	@mix deps.get
	@mix test

test: spec

### PUBLISH

publish: version docs publish-code publish-docs

publish-code: all
	@mix hex.publish

publish-docs: docs
	@MIX_ENV=docs mix hex.docs

### HELPERS/TOOLS

check-cc:
	@hash clang 2>/dev/null || \
	hash gcc 2>/dev/null || ( \
	echo '`clang` or `gcc` seem not to be installed or in your PATH.' && \
	echo 'Maybe you need to install one of it first.' && \
	exit 1)

docs:
	@MIX_ENV=docs mix docs

version:
	@cat VERSION

### CLEAN UP

clean: clean-objects clean-dirs

clean-objects:
	rm -f $(C_SRC_O_FILES)

clean-dirs:
	rm -rf $(BUILD_DIR) $(DEPS_DIR) $(PRIV_DIR) $(TMP_DIR)

### DEVELOPMENT

dev-prepare: dev-prebuilt-lib dev-copy-code dev-copy-license dev-spec-dump

$(CMARK_SRC_DIR):
	@mkdir -p $(TMP_DIR)
	@git clone --depth 1 $(CMARK_SRC_REPO) $@

dev-copy-code: $(C_SRC_DIR)
	cp \
		$(CMARK_C_FILES) \
		$(CMARK_H_FILES) \
		$(CMARK_INC_FILES) \
		$(CMARK_BUILD_DIR)/src/config.h \
		$(CMARK_BUILD_DIR)/src/cmark_export.h \
		$(CMARK_BUILD_DIR)/src/cmark_version.h \
	$(C_SRC_DIR)/

dev-copy-license: $(C_SRC_DIR)
	cp \
	$(CMARK_SRC_DIR)/COPYING \
	$(C_SRC_DIR)/

dev-prebuilt-lib: $(CMARK_SRC_DIR)
	mkdir -p $(CMARK_BUILD_DIR) && cd $(CMARK_BUILD_DIR) && cmake .. && $(MAKE)

dev-build-objects: dev-copy-code build-objects

$(CMARK_SPECS_JSON): dev-spec-dump

$(CMARK_SPECS_DIR):
	@mkdir -p $(TMP_DIR)
	@git clone --depth 1 $(CMARK_SPECS_REPO) $@

dev-spec-dump: $(CMARK_SPECS_DIR)
	@$(PYTHON) $(CMARK_SPECS_RUNNER) \
	--spec $(CMARK_SPECS_FILE) \
	--dump-tests | \
	jq -r -M -S "." > $(CMARK_SPECS_JSON) \
	|| true

### PHONY

.PHONY: all all-dev all-dev-test all-test check-cc clean dev-build-objects dev-copy-code dev-copy-license dev-prebuilt-lib dev-prepare dev-spec-dump docs spec test $(CMARK)<commit_msg>Adjust Makefile for specs JSON dump<commit_after>CC?=clang
ERLANG_PATH:=$(shell erl -eval 'io:format("~s~n", [lists:concat([code:root_dir(), "/erts-", erlang:system_info(version), "/include"])])' -s init stop -noshell)
ERLANG_FLAGS?=-I$(ERLANG_PATH)
EBIN_DIR?=ebin

NOOUT=2>&1 >/dev/null

BUILD_DIR=_build
DEPS_DIR=deps
PRIV_DIR=priv
SRC_DIR=src
TEST_DIR=test
TMP_DIR=tmp

CMARK=cmark

CMARK_SRC_REPO=https://github.com/jgm/cmark.git
CMARK_SRC_DIR=$(TMP_DIR)/$(CMARK)
CMARK_C_SRC_DIR=$(CMARK_SRC_DIR)/$(SRC_DIR)
CMARK_C_FILES=$(sort $(filter-out %main.c %print.c, $(wildcard $(CMARK_C_SRC_DIR)/*.c)))
CMARK_H_FILES=$(sort $(wildcard $(CMARK_C_SRC_DIR)/*.h))
CMARK_INC_FILES=$(sort $(wildcard $(CMARK_C_SRC_DIR)/*.inc))
CMARK_BUILD_DIR=$(CMARK_SRC_DIR)/build

PYTHON=python3
CMARK_SPECS_DIR=$(CMARK_SRC_DIR)/test
CMARK_SPECS_FILE=$(CMARK_SPECS_DIR)/spec.txt
CMARK_SPECS_RUNNER=$(CMARK_SPECS_DIR)/spec_tests.py
CMARK_SPECS_JSON=$(TEST_DIR)/$(CMARK)_specs.json

C_SRC_DIR=c_src
C_SRC_C_FILES=$(sort $(wildcard $(C_SRC_DIR)/*.c))
C_SRC_O_FILES=$(C_SRC_C_FILES:.c=.o)

NIF_SRC=$(SRC_DIR)/$(CMARK)_nif.c
NIF_LIB=$(PRIV_DIR)/$(CMARK).so

OPTIONS=-shared
ifeq ($(shell uname),Darwin)
OPTIONS+= -dynamiclib -undefined dynamic_lookup
endif
INCLUDES=-I$(C_SRC_DIR)

OPTFLAGS?=-fPIC -std=c99 -Wall
CFLAGS=-O2 $(OPTFLAGS) $(INCLUDES)
CMARK_OPTFLAGS=-DNDEBUG

### TARGETS

all: version check-cc $(NIF_LIB)

all-dev: dev-prepare all

all-test: all test

all-dev-test: all-dev test

build-objects: $(C_SRC_O_FILES)

$(C_SRC_DIR)/%.o : $(C_SRC_DIR)/%.c
	$(CC) $(CMARK_OPTFLAGS) $(CFLAGS) -o $@ -c $<

$(C_SRC_DIR):
	mkdir -p $@

$(PRIV_DIR):
	@mkdir -p $@ $(NOOUT)

$(NIF_LIB): $(PRIV_DIR) $(C_SRC_O_FILES)
	$(CC) $(CFLAGS) $(ERLANG_FLAGS) $(OPTIONS) $(C_SRC_O_FILES) $(NIF_SRC) -o $@

$(CMARK):
	@mix deps.get
	@mix compile

### TEST

spec: all $(CMARK_SPECS_JSON)
	@mix deps.get
	@mix test

test: spec

### PUBLISH

publish: version docs publish-code publish-docs

publish-code: all
	@mix hex.publish

publish-docs: docs
	@MIX_ENV=docs mix hex.docs

### HELPERS/TOOLS

check-cc:
	@hash clang 2>/dev/null || \
	hash gcc 2>/dev/null || ( \
	echo '`clang` or `gcc` seem not to be installed or in your PATH.' && \
	echo 'Maybe you need to install one of it first.' && \
	exit 1)

docs:
	@MIX_ENV=docs mix docs

version:
	@cat VERSION

### CLEAN UP

clean: clean-objects clean-dirs

clean-objects:
	rm -f $(C_SRC_O_FILES)

clean-dirs:
	rm -rf $(BUILD_DIR) $(DEPS_DIR) $(PRIV_DIR) $(TMP_DIR)

### DEVELOPMENT

dev-prepare: dev-prebuilt-lib dev-copy-code dev-copy-license dev-spec-dump

$(CMARK_SRC_DIR):
	@mkdir -p $(TMP_DIR)
	@git clone --depth 1 $(CMARK_SRC_REPO) $@

dev-copy-code: $(C_SRC_DIR)
	cp \
		$(CMARK_C_FILES) \
		$(CMARK_H_FILES) \
		$(CMARK_INC_FILES) \
		$(CMARK_BUILD_DIR)/src/config.h \
		$(CMARK_BUILD_DIR)/src/cmark_export.h \
		$(CMARK_BUILD_DIR)/src/cmark_version.h \
	$(C_SRC_DIR)/

dev-copy-license: $(C_SRC_DIR)
	cp \
	$(CMARK_SRC_DIR)/COPYING \
	$(C_SRC_DIR)/

dev-prebuilt-lib: $(CMARK_SRC_DIR)
	mkdir -p $(CMARK_BUILD_DIR) && cd $(CMARK_BUILD_DIR) && cmake .. && $(MAKE)

dev-build-objects: dev-copy-code build-objects

$(CMARK_SPECS_JSON): dev-spec-dump

dev-spec-dump: $(CMARK_SRC_DIR)
	@$(PYTHON) $(CMARK_SPECS_RUNNER) \
	--spec $(CMARK_SPECS_FILE) \
	--dump-tests | \
	jq -r -M -S "." > $(CMARK_SPECS_JSON) \
	|| true

### PHONY

.PHONY: all all-dev all-dev-test all-test check-cc clean dev-build-objects dev-copy-code dev-copy-license dev-prebuilt-lib dev-prepare dev-spec-dump docs spec test $(CMARK)
{'fraction_non_alphanumeric': 0.18072457686389704, 'fraction_numerical': 0.0029374737725556023, 'mean_word_length': 5.028667790893761, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6353665', 'n_tokens_mistral': 3257, 'n_tokens_neox': 3138, 'n_words': 554}