[書きかけ] libredwg を AWS Lambda レイヤーで使うためにビルド

# 仮想マシン

Amazon Linux 2 で環境構築します。

# クローン

```sh
git clone https://github.com/LibreDWG/libredwg.git
```

## 手修正

気に入らない部分を手修正します。

- makeinfo がない、Convert::Binary::C がない、という、ないない病を回避するために `doc` をビルドから省きます
- `programs/Makefile.am:47: warning: compiling 'dwggrep.c' with per-target flags requires 'AM_PROG_CC_C_O' in 'configure.ac'` という事なので `AM_PROG_CC_C_O` を追加します

```diff
diff --git a/Makefile.am b/Makefile.am
index 49b9ba14..d4148c35 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,7 +19,7 @@

 ACLOCAL_AMFLAGS = -I m4

-SUBDIRS = src programs examples test doc
+SUBDIRS = src programs examples test
 if HAVE_SWIG_PYTHON
 if !DISABLE_BINDINGS
 SUBDIRS += bindings
diff --git a/autogen.sh b/autogen.sh
old mode 100644
new mode 100755
diff --git a/configure.ac b/configure.ac
index 018033c3..b601e192 100644
--- a/configure.ac
+++ b/configure.ac
@@ -48,6 +48,7 @@ AC_CANONICAL_HOST
 dnl Checks for programs
 PKG_PROG_PKG_CONFIG
 AC_PROG_CC
+AM_PROG_CC_C_O
 AC_PROG_LN_S
 AC_PROG_MAKE_SET
 AC_PROG_INSTALL
diff --git a/src/config.h.in b/src/config.h.in
index 726d5837..acccadb8 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -134,9 +134,13 @@
    slash. */
 #undef LSTAT_FOLLOWS_SLASHED_SYMLINK

-/* Define to the sub-directory where libtool stores uninstalled libraries. */
+/* Define to the sub-directory in which libtool stores uninstalled libraries.
+   */
 #undef LT_OBJDIR

+/* Define to 1 if your C compiler doesn't accept -c and -o together. */
+#undef NO_MINUS_C_MINUS_O
+
 /* Define to the address where bug reports for this package should be sent. */
 #undef PACKAGE_BUGREPORT
```

# ビルド執行から zip まで

```sh
autoreconf

# configure.ac:51: error: required file 'build-aux/compile' not found
# configure.ac:51:   'automake --add-missing' can install 'compile'
automake --add-missing

autoreconf

# https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/configuration-layers.html によると、
# > レイヤーは、関数実行環境の /opt ディレクトリに抽出されます。
# > ランタイムごとに、言語に応じて、/opt のさまざまな場所のライブラリが検索されます。
./configure --prefix=/opt

make

make install DESTDIR=~/libredwg-build

cd ~/libredwg-build/opt

# デバッグ情報をそぎ落とす
strip bin/* lib/*.a

zip -r ~/libredwg-build.zip .
```

# AWS Lambda の Node.js 10.x の event 引数の中身

API Gateway の メソッドテストにて

クエリ文字列

```plaintext
key=value
```

ヘッダー
```plaintext
X-Header:Value1
X-Header:Value2
X-Header:Value3
```

応答

```json
{
  "resource": "/dwg-converter",
  "path": "/dwg-converter",
  "httpMethod": "POST",
  "headers": {
    "X-Header": "Value3"
  },
  "multiValueHeaders": {
    "X-Header": [
      "Value1",
      "Value2",
      "Value3"
    ]
  },
  "queryStringParameters": {
    "key": "value"
  },
  "multiValueQueryStringParameters": {
    "key": [
      "value"
    ]
  },
  "pathParameters": null,
  "stageVariables": null,
  "requestContext": {
    "resourceId": "XXX",
    "resourcePath": "/dwg-converter",
    "httpMethod": "POST",
    "extendedRequestId": "XXX",
    "requestTime": "04/Sep/2019:13:47:57 +0000",
    "path": "/dwg-converter",
    "accountId": "123",
    "protocol": "HTTP/1.1",
    "stage": "test-invoke-stage",
    "domainPrefix": "testPrefix",
    "requestTimeEpoch": 123,
    "requestId": "XXX",
    "identity": {
      "cognitoIdentityPoolId": null,
      "cognitoIdentityId": null,
      "apiKey": "test-invoke-api-key",
      "principalOrgId": null,
      "cognitoAuthenticationType": null,
      "userArn": "XXX",
      "apiKeyId": "test-invoke-api-key-id",
      "userAgent": "aws-internal/3 aws-sdk-java/1.11.590 Linux/4.9.184-0.1.ac.235.83.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.212-b03 java/1.8.0_212 vendor/Oracle_Corporation",
      "accountId": "123",
      "caller": "123",
      "sourceIp": "test-invoke-source-ip",
      "accessKey": "XXX",
      "cognitoAuthenticationProvider": null,
      "user": "123"
    },
    "domainName": "testPrefix.testDomainName",
    "apiId": "XXX"
  },
  "body": "本文です",
  "isBase64Encoded": false
}
```

[API Gateway+Lambdaでバイナリデータを処理](https://qiita.com/akatsukaha/items/024fbabc9a321665cb23) が続きとして完ぺきに近い…

適当過ぎて不完全ですが参考までに。

```js
exports.handler = (event) => {
    return new Promise((resolve, reject) => {
        const fs = require('fs');
        fs.writeFile(
            '/tmp/input.dwg',
            new Buffer(event.body, 'base64'),
            (err) => {
                if (err) {
                    resolve({
                        statusCode: 500,
                        body: JSON.stringify('Conversion failed!')
                    });
                }
                else {
                    const { exec } = require('child_process');
                    exec('dwg2SVG input.dwg', {
                        cwd: "/tmp"
                    }, (error, stdout, stderr) => {
                        console.log(error);
                        console.log(stdout);
                        console.error(stderr);
                        const response = {
                            statusCode: 200,
                            body: JSON.stringify({ error, stdout, stderr })
                        };
                        resolve(response);
                    });
                }
            });
    });
};
```