Skip to content

Commit

Permalink
cli
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Jan 13, 2024
1 parent f95af87 commit ba4edca
Show file tree
Hide file tree
Showing 38 changed files with 133,455 additions and 2,670 deletions.
93 changes: 92 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
FROM --platform=linux/amd64 amd64/ubuntu:noble

# WORKDIR /root

ENV TERM linux
ENV DEBIAN_FRONTEND noninteractive

Expand All @@ -14,11 +17,21 @@ RUN apt-get upgrade -y -q

RUN apt-get -y install build-essential
RUN apt-get -y install curl

# RUN curl -s https://archive.swiftlang.xyz/install.sh | bash
# RUN apt install swiftlang
# RUN swift -v

RUN apt-get -y install exiftool
RUN apt-get -y install llvm-17
RUN apt-get -y install clang-17
RUN apt-get -y install clang-format-17
RUN apt-get -y install imagemagick
RUN apt-get -y install software-properties-common

RUN apt-get update -y -q
RUN apt-get upgrade -y -q

# RUN apt-get -y --fix-missing install software-properties-common
RUN apt-get -y install fontforge
RUN apt-get -y install pandoc
RUN apt-get -y install texlive
Expand All @@ -38,6 +51,72 @@ RUN apt-get -y install rustfmt
RUN apt-get -y install wget
RUN apt-get -y install gnupg

RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install binutils
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install git
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install unzip
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install gnupg2
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install libc6-dev
# RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install libcurl4-openssl-dev
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install libedit2
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install libgcc-11-dev
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install libpython3-dev
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install libsqlite3-0
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install libstdc++-11-dev
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install libxml2-dev
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install libz3-dev
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install pkg-config
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install python3-lldb
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install tzdata
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && apt-get -q -y install zlib1g-dev
RUN rm -r /var/lib/apt/lists/*

# Install swift
ARG SWIFT_SIGNING_KEY=A62AE125BBBFBB96A6E042EC925CC1CCED3D1561
ARG SWIFT_PLATFORM=ubuntu22.04
ARG SWIFT_BRANCH=swift-5.9.2-release
ARG SWIFT_VERSION=swift-5.9.2-RELEASE
ARG SWIFT_WEBROOT=https://download.swift.org

ENV SWIFT_SIGNING_KEY=$SWIFT_SIGNING_KEY \
SWIFT_PLATFORM=$SWIFT_PLATFORM \
SWIFT_BRANCH=$SWIFT_BRANCH \
SWIFT_VERSION=$SWIFT_VERSION \
SWIFT_WEBROOT=$SWIFT_WEBROOT

RUN set -e; \
ARCH_NAME="$(dpkg --print-architecture)"; \
url=; \
case "${ARCH_NAME##*-}" in \
'amd64') \
OS_ARCH_SUFFIX=''; \
;; \
'arm64') \
OS_ARCH_SUFFIX='-aarch64'; \
;; \
*) echo >&2 "error: unsupported architecture: '$ARCH_NAME'"; exit 1 ;; \
esac; \
SWIFT_WEBDIR="$SWIFT_WEBROOT/$SWIFT_BRANCH/$(echo $SWIFT_PLATFORM | tr -d .)$OS_ARCH_SUFFIX" \
&& SWIFT_BIN_URL="$SWIFT_WEBDIR/$SWIFT_VERSION/$SWIFT_VERSION-$SWIFT_PLATFORM$OS_ARCH_SUFFIX.tar.gz" \
&& SWIFT_SIG_URL="$SWIFT_BIN_URL.sig" \
# - Grab curl here so we cache better up above
&& export DEBIAN_FRONTEND=noninteractive \
&& apt-get -q update && apt-get -q install -y curl && rm -rf /var/lib/apt/lists/* \
# - Download the GPG keys, Swift toolchain, and toolchain signature, and verify.
&& export GNUPGHOME="$(mktemp -d)" \
&& curl -fsSL "$SWIFT_BIN_URL" -o swift.tar.gz "$SWIFT_SIG_URL" -o swift.tar.gz.sig \
&& gpg --batch --quiet --keyserver keyserver.ubuntu.com --recv-keys "$SWIFT_SIGNING_KEY" \
&& gpg --batch --verify swift.tar.gz.sig swift.tar.gz \
# - Unpack the toolchain, set libs permissions, and clean up.
&& tar -xzf swift.tar.gz --directory / --strip-components=1 \
&& chmod -R o+r /usr/lib/swift \
&& rm -rf "$GNUPGHOME" swift.tar.gz.sig swift.tar.gz \
&& apt-get purge --auto-remove -y curl

# Reinstall curl?
RUN apt-get update -y -q
RUN apt-get upgrade -y -q
RUN apt-get -y install curl

# Install Node.js
# https://stackoverflow.com/questions/25899912/how-to-install-nvm-in-docker
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Expand All @@ -61,6 +140,7 @@ RUN go install github.com/klauspost/asmfmt/cmd/asmfmt@ef134b9cec704e2b7b336fb021
RUN /home/python/venv/bin/jill install --preferred-arch arm64 -c

# Setup basic aliases
RUN rm /usr/bin/clang
RUN ln -s /usr/bin/clang-17 /usr/bin/clang
RUN ln -s /usr/bin/clang-format-17 /usr/bin/clang-format
RUN ln -s /usr/bin/llc-17 /usr/bin/llc
Expand All @@ -69,3 +149,14 @@ RUN ln -s /home/python/venv/bin/black /usr/bin/black
LABEL org.opencontainers.image.source https://github.com/termsurf/task
LABEL org.opencontainers.image.title "Task: Common Actions Interface"
LABEL org.opencontainers.image.description "A wrapper around a lot of tools to make it easier to use them all."

RUN swift --version
COPY test .
RUN git clone https://github.com/apple/swift-format.git
RUN cd swift-format && git checkout "tags/509.0.0" && swift build -c release
# RUN ls -la /usr/bin
# RUN ls -s $(cd swift-format && swift build --show-bin-path -c release)/swift-format /usr/bin/swift-format
RUN cp /swift-format/.build/x86_64-unknown-linux-gnu/release/swift-format /usr/local/bin
# RUN swift-format -v
RUN apt-get -y install unoconv
# doc2pdf respondus-docx-sample-file.docx
5 changes: 2 additions & 3 deletions Dockerfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RUN apt-get -y install llvm-17
RUN apt-get -y install clang-17
RUN apt-get -y install clang-format-17
RUN apt-get -y install imagemagick
RUN apt-get -y install software-properties-common
RUN apt-get -y --fix-missing install software-properties-common
RUN apt-get -y install fontforge
RUN apt-get -y install pandoc
RUN apt-get -y install texlive
Expand Down Expand Up @@ -149,5 +149,4 @@ RUN swift --version
COPY test .
RUN git clone https://github.com/apple/swift-format.git
RUN cd swift-format && git checkout "tags/509.0.0" && swift build -c release
RUN echo "LANCE"
RUN swift-format -v
# RUN swift-format -v
83 changes: 81 additions & 2 deletions code/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,84 @@
#!/usr/bin/env node

const cli = {}
import _ from 'lodash'
import { Input, Value } from './type'
import * as task from './task'

export default cli
call(process.argv)

/**
* Parse the input into an action and state.
*/

function call(argv: Array<string>) {
const input = read(argv)

switch (input.action) {
// case 'compress':
// return build.compress(input)
case 'convert':
return task.convert(input)
// case 'replace':
// return build.replace(input)
// case 'create':
// return build.create(input)
// case 'remove':
// return build.remove(input)
// case 'rename':
// return build.rename(input)
// case 'resize':
// return build.resize(input)
// case 'update':
// return build.update(input)
// case 'upload':
// return build.upload(input)
// case 'build':
// return build.build(input)
// case 'slice':
// return build.slice(input)
// case 'split':
// return build.split(input)
// case 'read':
// return build.read(input)
// case 'add':
// return build.add(input)
default:
throw new Error(`Action ${input.action} undefined`)
}
}

/**
* Generically fetch CLI args.
*/

function read(argv: Array<string>) {
let i = 2
const input: Input = {
action: argv[i++],
object: [],
detail: {},
}

while (i < argv.length) {
const item = argv[i++]
if (item.match(/^-+/)) {
const name = item
const cased = name.match(/^--/) ? _.camelCase(name) : name
let value: Value = argv[i++]
if (!value) {
value = true
} else if (value.match(/^-+/)) {
i--
value = true
}
input.detail[cased] = input.detail[cased] || []
input.detail[cased].push(value)
} else {
input.object.push(item)
}
}

return input
}

console.log(process.argv)
53 changes: 53 additions & 0 deletions code/cli/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Input, Link, Value } from './type'
import fs from 'fs'
import path from 'path'
import call, { convertImageWithImageMagick } from '../node'

export const CONVERT_KEY: Record<string, Link> = {
'-i': { name: 'inputPath' },
'-o': { name: 'outputPath', need: false },
'-I': { name: 'inputExtension', need: false },
'-O': { name: 'outputExtension', need: false },
'-x': { name: 'architecture', need: false },
'--arch': { name: 'architecture', need: false },
'-s': { name: 'syntax', need: false },
'-f': { name: 'prettify', need: false },
'-h': { name: 'help', need: false, like: 'boolean' },
}

export async function convert(source: Input) {
const input = makeInput(source, CONVERT_KEY)
if (input.help) {
console.log(`
call:
task convert -i <input-path> -o <output-path>
`)
} else {
const call = (await import('../node')).default
await call('convert', input)
}
}

function makeInput(source: Input, map: Record<string, Link>) {
const out: Record<string, Value | Array<Value>> = {}
for (const name in source.detail) {
const form = map[name]
const mapName = form?.name ?? name
const detail = source.detail[name].map(v => {
switch (form?.like) {
case 'string':
return v
case 'integer':
return parseInt(v as string, 10)
case 'decimal':
return parseFloat(v as string)
}
return v
})
const val = form?.list ? detail : detail[0]
out[mapName] = val
}
return out
}
14 changes: 14 additions & 0 deletions code/cli/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type Input = {
action: string
object: Array<string>
detail: Record<string, Array<Value>>
}

export type Value = string | boolean | number

export type Link = {
name: string
list?: boolean
need?: boolean
like?: string
}
58 changes: 12 additions & 46 deletions code/node/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,9 @@ import {
IOPath,
LLVM_ARCHITECTURE_CONTENT,
LlvmArchitecture,
LlvmOptimizationLevel,
} from '../shared/type'
import { exec } from './process'

// ; ModuleID = 'tmp/c.c'
// source_filename = "tmp/c.c"
// target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
// target triple = "arm64-apple-macosx14.0.0"
// @.str = private unnamed_addr constant [14 x i8] c"Hello, World!\00", align 1
export async function generateLlvmIrFromC({
inputPath,
outputPath,
optimizationLevel = '0',
fastMath = false,
}: IOPath & {
optimizationLevel?: LlvmOptimizationLevel
fastMath?: boolean
}) {
const cmd = [`clang -S -emit-llvm -O${optimizationLevel}`]

if (fastMath) {
cmd.push(`-ffast-math`)
}

cmd.push(`-o "${outputPath}" "${inputPath}"`)

return await exec(cmd.join(' '))
}

export async function generateLlvmIrFromCPP({
inputPath,
outputPath,
optimizationLevel = '0',
fastMath = false,
}: IOPath & {
optimizationLevel?: LlvmOptimizationLevel
fastMath?: boolean
}) {
const cmd = [`clang++ -S -emit-llvm -O${optimizationLevel}`]

if (fastMath) {
cmd.push(`-ffast-math`)
}

cmd.push(`-o "${outputPath}" "${inputPath}"`)

return await exec(cmd.join(' '))
}

// CSharp: .cs
// Java: .java
// JavaScript: .mjs .js .ts
Expand Down Expand Up @@ -121,6 +75,18 @@ export async function generateAssemblyFromLlvmIr({
// rustc --emit=llvm-ir
// cargo rustc -- --emit=llvm-ir

export async function buildCommandToDecompileBinary() {
const cmd = [
`objdump`,
`-S`,
// `--x86-asm-syntax=intel`,
`-M`,
`intel`,
// `"${inputPath}"`,
]
return cmd
}

// objdump -S --x86-asm-syntax=intel example.out
// objdump -f assert
// objdump -x assert
Expand Down
Loading

0 comments on commit ba4edca

Please sign in to comment.