Skip to content

Commit f9e55c9

Browse files
committed
feat(haskell): docker to compile haskell
1 parent c511e41 commit f9e55c9

File tree

7 files changed

+131
-12
lines changed

7 files changed

+131
-12
lines changed

2024/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
*
22
!/**/
33
!*.*
4+
!Makefile
5+
!Dockerfile
46

57
*.hi
68
*.o

2024/Day06/Makefile

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
CONTAINER_NAME=haskell-aoc
22
SRC=./Day06.hs
33
TARGET=Day06
44
all: $(TARGET)
@@ -9,4 +9,17 @@ $(TARGET): $(SRC)
99
clean:
1010
$(RM) ./Day06 ./Day06.o ./Day06.hi
1111

12-
.PHONY: all $(TARGET) clean
12+
setup: $(SRC)
13+
docker cp $(SRC) $(CONTAINER_NAME):/home/haskell/Main.hs
14+
docker cp input.txt $(CONTAINER_NAME):/home/haskell/input.txt
15+
docker cp shortinput.txt $(CONTAINER_NAME):/home/haskell/shortinput.txt
16+
17+
profiling: setup
18+
docker exec -it ${CONTAINER_NAME} cabal build --enable-profiling
19+
docker exec -it ${CONTAINER_NAME} ./profile input.txt
20+
21+
run: setup
22+
docker exec -it ${CONTAINER_NAME} cabal build
23+
docker exec -it ${CONTAINER_NAME} ./run input.txt
24+
25+
.PHONY: all $(TARGET) clean profiling

Dockerfile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM haskell:9.6.6
2+
3+
# Install best text editor
4+
RUN apt-get update && apt-get install -y vim
5+
6+
RUN cabal update
7+
8+
# Copy base project to force cabal to install dependencies in the image
9+
COPY ./haskell /home/haskell
10+
11+
WORKDIR /home/haskell
12+
13+
# Installing the program's dependencies
14+
RUN cabal build
15+
16+
# Installing the profiling dependencies
17+
RUN cabal build --enable-profiling
18+
19+
RUN echo '#!/bin/sh\n ./dist-newstyle/build/x86_64-linux/ghc-9.6.6/Main-0.1.0.0/x/Main/build/Main/Main +RTS -p -RTS $@' > profile && chmod u+x profile
20+
RUN echo '#!/bin/sh\n ./dist-newstyle/build/x86_64-linux/ghc-9.6.6/Main-0.1.0.0/x/Main/build/Main/Main $@' > run && chmod u+x run
21+
22+
CMD ["bash"]

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
11
# Advent-of-code
22
Advent of code, probably be in several languages for fun
33

4+
## Use
5+
6+
### A new day :
7+
8+
Execute the `build_day.sh` file to generate the necessary files depending on the chosen language
9+
10+
You need to execute it like
11+
12+
```sh
13+
. build_day.sh [hs]
14+
```
15+
16+
### Profiling :
17+
18+
Build the Haskell Docker Image:
19+
20+
```sh
21+
docker build -t haskell-aoc .
22+
23+
```
24+
25+
Create a haskell Docker
26+
```sh
27+
docker run -it --rm --name haskell-aoc haskell-aoc
28+
```
29+
Keep it running in the background in order to launch your solutions on it
30+
431
## 2021
532

633
Started coding in Shell, was too difficult, so finished in Python..
734

835
## 2022
936

1037
Starting in shell and Haskell, will see how it goes
38+
39+
## 2023
40+
41+
## 2024
42+
43+
Continuing in haskell
44+
45+
Having fun trying to automate stuff as well

build_day.sh

+30-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ is_sourced() {
1010
}
1111

1212
if ! `is_sourced`; then
13-
echo "use as '. $0'"
13+
echo "use as '. $0 [hs]'"
1414
echo exit 1
1515
fi
1616

@@ -46,11 +46,11 @@ type Output = Int
4646
parseInput :: String -> Input
4747
parseInput = lines
4848
49-
-- part1 :: Input -> Output
50-
-- part1 input =
49+
part1 :: Input -> Output
50+
part1 input = -1
5151
52-
-- part2 :: Input -> Output
53-
-- part2 input =
52+
part2 :: Input -> Output
53+
part2 input = -1
5454
5555
main :: IO ()
5656
main = do
@@ -60,30 +60,50 @@ main = do
6060
6161
print input
6262
63-
-- print $ part1 input
64-
-- print $ part2 input
63+
print $ part1 input
64+
print $ part2 input
6565
EOF
6666

6767
cat <<EOF > $FOLDER/Makefile
68-
68+
CONTAINER_NAME=haskell-aoc
6969
SRC=./Day$DAY.hs
7070
TARGET=Day$DAY
7171
all: \$(TARGET)
7272
7373
\$(TARGET): \$(SRC)
74-
ghc \$(SRC)
74+
ghc -O3 \$(SRC)
75+
76+
profile: \$(SRC)
77+
7578
7679
clean:
7780
\$(RM) ./Day$DAY ./Day$DAY.o ./Day$DAY.hi
7881
79-
.PHONY: all \$(TARGET) clean
82+
setup: \$(SRC)
83+
docker cp \$(SRC) \$(CONTAINER_NAME):/home/haskell/Main.hs
84+
docker cp input.txt \$(CONTAINER_NAME):/home/haskell/input.txt
85+
docker cp shortinput.txt \$(CONTAINER_NAME):/home/haskell/shortinput.txt
86+
87+
profiling: setup
88+
docker exec -it \$(CONTAINER_NAME) cabal build --enable-profiling
89+
docker exec -it \$(CONTAINER_NAME) ./profile input.txt
90+
91+
run: setup
92+
docker exec -it \$(CONTAINER_NAME) cabal build
93+
docker exec -it \$(CONTAINER_NAME) ./run input.txt
94+
95+
.PHONY: all \$(TARGET) clean setup profiling run
8096
EOF
8197

8298
;;
8399

84100
"")
85101
echo "No language specified"
86102
;;
103+
*)
104+
echo "Unknown language specified"
105+
;;
106+
87107
esac
88108

89109
cd $FOLDER

haskell/Main.cabal

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cabal-version: 2.4
2+
name: Main
3+
version: 0.1.0.0
4+
license: NONE
5+
build-type: Simple
6+
common warnings
7+
ghc-options: -Wall -O2
8+
9+
executable Main
10+
import: warnings
11+
12+
main-is: Main.hs
13+
14+
build-depends: base ^>=4.18.2.1,
15+
containers >=0.6.7,
16+
matrix >=0.3.6.3,
17+
parallel >=3.2.2.0
18+
19+
-- Directories containing source files.
20+
hs-source-dirs: .
21+
22+
-- Base language which the package is written in.
23+
default-language: GHC2021

haskell/Main.hs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
main :: IO()
3+
main = do
4+
print 42

0 commit comments

Comments
 (0)