# Building from Source

This guide explains how to build KayakNet from source code.

## Prerequisites

### Required

* **Go 1.21+** - [Download Go](https://go.dev/dl/)
* **Git** - [Download Git](https://git-scm.com/)

### Optional

* **Make** - For build automation
* **Docker** - For containerized builds

## Clone Repository

```bash
git clone https://github.com/KayakNet/KayakNet.git
cd KayakNet
```

## Build

### Simple Build

```bash
go build -o kayakd ./cmd/kayakd/
```

### Optimized Build

```bash
go build -ldflags="-s -w" -o kayakd ./cmd/kayakd/
```

Flags:

* `-s` - Strip symbol table
* `-w` - Strip DWARF debugging info

Result: \~40% smaller binary

### Cross-Compilation

**Windows (from Linux/macOS):**

```bash
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o kayakd.exe ./cmd/kayakd/
```

**Linux (from Windows/macOS):**

```bash
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o kayakd ./cmd/kayakd/
```

**macOS (from Linux/Windows):**

```bash
GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o kayakd ./cmd/kayakd/
```

**ARM (Raspberry Pi):**

```bash
GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o kayakd-arm64 ./cmd/kayakd/
```

## Build All Platforms

```bash
# Build script
#!/bin/bash
VERSION="v0.1.28"

# Linux
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o kayakd-${VERSION}-linux-amd64 ./cmd/kayakd/

# Windows  
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o kayakd-${VERSION}-windows-amd64.exe ./cmd/kayakd/

# macOS
GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o kayakd-${VERSION}-darwin-amd64 ./cmd/kayakd/

# Create archives
zip kayakd-${VERSION}-linux-amd64.zip kayakd-${VERSION}-linux-amd64
zip kayakd-${VERSION}-windows-amd64.zip kayakd-${VERSION}-windows-amd64.exe
zip kayakd-${VERSION}-darwin-amd64.zip kayakd-${VERSION}-darwin-amd64
```

## Project Structure

```
KayakNet/
├── cmd/
│   └── kayakd/           # Main binary entry point
│       └── main.go
├── internal/
│   ├── chat/             # Chat system
│   ├── config/           # Configuration
│   ├── crypto/           # Cryptographic operations
│   ├── dht/              # Distributed hash table
│   ├── e2e/              # End-to-end encryption
│   ├── escrow/           # Escrow system
│   ├── identity/         # Node identity
│   ├── market/           # Marketplace
│   ├── names/            # .kyk domain system
│   ├── network/          # Core networking
│   ├── onion/            # Onion routing
│   ├── pow/              # Proof of work
│   └── pubsub/           # Publish-subscribe
├── pkg/
│   └── protocol/         # Wire protocol
├── android/              # Android app source
├── go.mod
├── go.sum
└── README.md
```

## Dependencies

View dependencies:

```bash
go mod graph
```

Update dependencies:

```bash
go get -u ./...
go mod tidy
```

## Testing

### Run Tests

```bash
go test ./...
```

### Run with Coverage

```bash
go test -cover ./...
```

### Run Specific Package

```bash
go test ./internal/chat/...
```

### Verbose Output

```bash
go test -v ./...
```

## Linting

### Install golangci-lint

```bash
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
```

### Run Linter

```bash
golangci-lint run
```

## Docker Build

### Dockerfile

```dockerfile
FROM golang:1.21-alpine AS builder

WORKDIR /app
COPY . .
RUN go build -ldflags="-s -w" -o kayakd ./cmd/kayakd/

FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /app/kayakd /usr/local/bin/
ENTRYPOINT ["kayakd"]
```

### Build Image

```bash
docker build -t kayaknet/kayakd:latest .
```

### Run Container

```bash
docker run -d \
  --name kayaknet \
  -p 4242:4242 \
  -p 8080:8080 \
  -p 8118:8118 \
  -v kayaknet-data:/data \
  kayaknet/kayakd:latest \
  --bootstrap 203.161.33.237:4242 --proxy
```

## Build Flags

| Flag                     | Description                    |
| ------------------------ | ------------------------------ |
| `-s`                     | Strip symbol table             |
| `-w`                     | Strip DWARF info               |
| `-X main.Version=v1.0.0` | Set version variable           |
| `-race`                  | Enable race detector (testing) |

### Set Version at Build

```bash
go build -ldflags="-s -w -X main.Version=v1.0.0" -o kayakd ./cmd/kayakd/
```

## Troubleshooting

### "go: command not found"

Install Go and add to PATH:

```bash
export PATH=$PATH:/usr/local/go/bin
```

### "cannot find package"

Download dependencies:

```bash
go mod download
```

### CGO Issues

Disable CGO for static builds:

```bash
CGO_ENABLED=0 go build ...
```

### Large Binary Size

Use ldflags and UPX:

```bash
go build -ldflags="-s -w" -o kayakd ./cmd/kayakd/
upx --best kayakd
```
