#edit-mode: -*- python -*-
#coding:utf-8

WORKROOT('../../../')

CUDA_VERSION("8.0")

#gcc version, default 'gcc'
COMPILER('gcc482')

#Preprocessor flags.
CPPFLAGS(r'-D_GNU_SOURCE -D__STDC_LIMIT_MACROS -D__const__= -Dtypeof=__typeof__ -DUSE_PTHREAD -DWITH_GPU')
#CPPFLAGS(r'-DVERSION=\"%s\"' % SVN_LAST_CHANGED_REV())

#C++ flags.
CXXFLAGS('-g -O2 -pipe -W -Wall -fPIC -fno-omit-frame-pointer -Wno-deprecated -Wno-unused-parameter -Wno-unused-variable -Wno-unused-local-typedefs -Wno-sign-compare -std=c++11')

#link flags
LDFLAGS(' -lpthread -lcrypto -lrt -lssl -ldl -lz')
LDFLAGS(' -L/opt/compiler/cuda-8.0/lib64 -lcudart')

INCPATHS('$INC')
INCPATHS('./')
INCPATHS('$OUT_ROOT/third-64/protobuf/output/include')

CONFIGS('third-64/protobuf@protobuf_3-1-0-6209_PD_BL')
CONFIGS('baidu/base/baidu-rpc@ci-base')
CONFIGS('baidu/third-party/gflags@v2.0.0@git_branch')
CONFIGS('baidu/third-party/glog@v0.3.x@git_branch')
CONFIGS('baidu/third-party/opencv@master@git_branch')
CONFIGS('baidu/third-party/mklml@v20180406@git_branch')
NEED_OUTPUT('baidu/third-party/mklml')
#CONFIGS('baidu/paddlepaddle/paddle@paddle_prebuilt_cuda-1-0-0-1_PD_BL@git_tag')

#LIBS('$WORK_ROOT/baidu/paddlepaddle/paddle/third_party/install/mklml/lib/libiomp5.so')
#LIBS('$WORK_ROOT/baidu/paddlepaddle/paddle/third_party/install/mklml/lib/libmklml_intel.so')
# LIBS('$WORK_ROOT/baidu/paddlepaddle/paddle/third_party/install/protobuf/lib/libprotobuf.a')
#LIBS('$WORK_ROOT/baidu/paddlepaddle/paddle/third_party/install/snappy/lib/libsnappy.a')
#LIBS('$WORK_ROOT/baidu/paddlepaddle/paddle/third_party/install/snappystream/lib/libsnappystream.a')
#LIBS('$WORK_ROOT/baidu/paddlepaddle/paddle/third_party/install/xxhash/lib/libxxhash.a')
#LIBS('$WORK_ROOT/baidu/paddlepaddle/paddle/third_party/install/zlib/lib/libz.a')
#
CONFIGS('baidu/paddlepaddle/gpu-infer-temporary@release_cuda8_cudnn7@git_branch')

LIBS('$WORK_ROOT/baidu/paddlepaddle/gpu-infer-temporary/third_party/install/mklml/lib/libiomp5.so')
LIBS('$WORK_ROOT/baidu/paddlepaddle/gpu-infer-temporary/third_party/install/mklml/lib/libmklml_intel.so')
# LIBS('$WORK_ROOT/baidu/paddlepaddle/gpu-infer-temporary/third_party/install/protobuf/lib/libprotobuf.a')
LIBS('$WORK_ROOT/baidu/paddlepaddle/gpu-infer-temporary/third_party/install/snappy/lib/libsnappy.a')
LIBS('$WORK_ROOT/baidu/paddlepaddle/gpu-infer-temporary/third_party/install/snappystream/lib/libsnappystream.a')
LIBS('$WORK_ROOT/baidu/paddlepaddle/gpu-infer-temporary/third_party/install/xxhash/lib/libxxhash.a')
LIBS('$WORK_ROOT/baidu/paddlepaddle/gpu-infer-temporary/third_party/install/zlib/lib/libz.a')


DELETE_AUTO_LIBS('$OUT_ROOT/third-64/gflags/output/lib/libgflags.a')
DELETE_AUTO_LIBS('$OUT_ROOT/third-64/gflags/output/lib/libgflags_nothreads.a')
#DELETE_AUTO_LIBS('$OUT_ROOT/baidu/third-party/gflags/output/lib/libgflags.a')

#pdcodegen
HEADERS(GLOB_GEN_SRCS('predictor/proto/pds_option.pb.h'), '$INC/pdcodegen')

pdcodegen_sources = GLOB('pdcodegen/plugin/*.cc')
pdcodegen_sources += ' predictor/proto/pds_option.proto'
pdcodegen_sources += ' pdcodegen/src/pdcodegen.cpp'

# DELETE_AUTO_LIBS('$OUT_ROOT/third-64/protobuf/output/lib/libprotobuf.a')
# DELETE_AUTO_LIBS('$OUT_ROOT/third-64/protobuf/output/lib/libprotobuf-lite.a')

Application('pdcodegen', Sources(pdcodegen_sources))

#sub directory
Directory('configure')

# predictor
predictor_sources = []
predictor_sources.append('predictor/common/*.cpp')
predictor_sources.append('predictor/op/*.cpp')
predictor_sources.append('predictor/framework/*.cpp')
predictor_sources.append('predictor/mempool/*.cpp')
predictor_sources.append('predictor/proto/*.proto')

HEADERS(GLOB_GEN_SRCS('predictor/proto/*.pb.h'), '$INC/predictor/')
HEADERS('predictor/common/*.h', '$INC/predictor/common')
HEADERS('predictor/framework/*.h', '$INC/predictor/framework')
HEADERS('predictor/mempool/*.h', '$INC/predictor/mempool')
HEADERS('predictor/op/*.h', '$INC/predictor/op')

StaticLibrary('pdserving', Sources(GLOB(' '.join(predictor_sources)), 'predictor/src/pdserving.cpp'))

# Sub directory

# inferencer-fluid-cpu
INCPATHS('.')
inferencer_fluid_cpu_sources = 'inferencer-fluid-cpu/src/fluid_cpu_engine.cpp'
StaticLibrary('inferencer-fluid-cpu', Sources(inferencer_fluid_cpu_sources))

Directory('inferencer-fluid-gpu')
Directory('sdk-cpp')

# demo-client
Directory('sdk-cpp')
INCPATHS('$INC')
INCPATHS('$INC/sdk-cpp')
INCPATHS('$INC/sdk-cpp/include')
HEADERS(GLOB_GEN_SRCS('configure/proto/sdk_configure.pb.h'), '$INC/sdk-cpp')
HEADERS(GLOB_GEN_SRCS('predictor/proto/builtin_format.pb.h'), '$INC/sdk-cpp')
HEADERS(GLOB_GEN_SRCS('predictor/proto/pds_option.pb.h'), '$INC/sdk-cpp')
HEADERS(GLOB_GEN_SRCS('demo-client/proto/*.pb.h'), '$INC/sdk-cpp')

LIBS('$OUT/lib/libpdconfigure.a')
Application('ximage', Sources('demo-client/src/ximage.cpp'), WholeArchives('$OUT/lib/libpdsdk-cpp.a'), Libs('$OUT/lib/libpdconfigure.a'))
Application('ximage_press', Sources('demo-client/src/ximage_press.cpp'), WholeArchives('$OUT/lib/libpdsdk-cpp.a'), Libs('$OUT/lib/libpdconfigure.a'))
Application('echo', Sources('demo-client/src/echo.cpp'), WholeArchives('$OUT/lib/libpdsdk-cpp.a'), Libs('$OUT/lib/libpdconfigure.a'))
Application('dense_format', Sources('demo-client/src/dense_format.cpp'), WholeArchives('$OUT/lib/libpdsdk-cpp.a'), Libs('$OUT/lib/libpdconfigure.a'))
Application('sparse_format', Sources('demo-client/src/sparse_format.cpp'), WholeArchives('$OUT/lib/libpdsdk-cpp.a'), Libs('$OUT/lib/libpdconfigure.a'))
Application('int64tensor_format', Sources('demo-client/src/int64tensor_format.cpp'), WholeArchives('$OUT/lib/libpdsdk-cpp.a'), Libs('$OUT/lib/libpdconfigure.a'))
Application('text_classification', Sources('demo-client/src/text_classification.cpp'), WholeArchives('$OUT/lib/libpdsdk-cpp.a'))
Application('text_classification_press', Sources('demo-client/src/text_classification_press.cpp'), WholeArchives('$OUT/lib/libpdsdk-cpp.a'), Libs('$OUT/lib/libpdconfigure.a'))
OUTPUT('demo-client/conf', '$OUT/demo/client')
OUTPUT('demo-client/data', '$OUT/demo/client')

# demo-serving
INCPATHS('$INC')
INCPATHS('$INC/predictor')
INCPATHS('./')
INCPATHS('./predictor/')
INCPATHS('$OUT_ROOT/third-64/protobuf/output/include')
INCPATHS('$OUT_ROOT/baidu/third-party/glog/output/include')
INCPATHS('$INC/predictor/proto')

PROTOFLAGS(
        '--plugin=protoc-gen-pdcodegen=./pdcodegen/plugin/pdcodegen',
        '--pdcodegen_out demo-serving/proto --proto_path=.',
        '--proto_path=./predictor/proto')

serving_sources = []
serving_sources.append('demo-serving/op/*.cpp')
serving_sources.append('demo-serving/proto/*.proto')

HEADERS(GLOB_GEN_SRCS('demo-serving/proto/*.pb.h'), '$INC/demo-serving')
Application('serving', Sources(GLOB(' '.join(serving_sources))), WholeArchives('$OUT/lib/libinferencer-fluid-gpu.a $OUT/lib/libinferencer-fluid-cpu.a $OUT/lib/libpdserving.a $OUT/lib/libpdconfigure.a'))

OUTPUT('demo-serving/conf', '$OUT/demo/serving/')
OUTPUT('demo-serving/data', '$OUT/demo/serving')
#OUTPUT('$WORK_ROOT/baidu/paddlepaddle/gpu-infer-temporary/third_party/install/mklml/lib/libiomp5.so', '$OUT/demo/serving/bin')
#OUTPUT('$WORK_ROOT/baidu/paddlepaddle/gpu-infer-temporary/third_party/install/mklml/lib/libmklml_intel.so', '$OUT/demo/serving/bin')
