diff --git a/configure.in b/configure.in
index c0b76f8..d10222f 100644
--- libreoffice-3.5.0/configure.in
+++ libreoffice-3.5.0/configure.in
@@ -7936,6 +7936,13 @@ fi
 AC_SUBST(GMAKE_PARALLELISM)
 AC_SUBST(GMAKE_MODULE_PARALLELISM)
 
+if test -x /usr/bin/dpkg-buildflags; then
+	CDEFAULTOPT=`dpkg-buildflags --get CFLAGS 2>/dev/null | grep -E -o "\-O[[0-3]]"`
+else
+	CDEFAULTOPT=-O2
+fi
+AC_SUBST(CDEFAULTOPT)
+
 dnl ===================================================================
 dnl Setting up the environment.
 dnl ===================================================================
diff --git a/set_soenv.in b/set_soenv.in
index ea9f10c..1b32f7f 100644
--- libreoffice-3.5.0/set_soenv.in
+++ libreoffice-3.5.0/set_soenv.in
@@ -1562,6 +1562,7 @@
 ToFile( "COMPATH",           $COMPATH,           "e" );
 ToFile( "CPU",               $CPU,               "e" );
 ToFile( "CPUNAME",           $CPUNAME,           "e" );
+ToFile( "CDEFAULTOPT",       "@CDEFAULTOPT@",    "e" );
 ToFile( "GUI",               $GUI,               "e" );
 ToFile( "GUIBASE",           $GUIBASE,           "e" );
 ToFile( "GVER",              $GVER,              "e" );
diff --git a/solenv/inc/unxgcc.mk b/solenv/inc/unxgcc.mk
index b54736c..4f7177d 100644
--- libreoffice-3.5.0/solenv/inc/unxgcc.mk
+++ libreoffice-3.5.0/solenv/inc/unxgcc.mk
@@ -31,7 +31,7 @@ ASM*=
 AFLAGS*=
 SOLAR_JAVA*=
 # default optimization level for product code
-CDEFAULTOPT*=-O2
+CDEFAULTOPT*=$(shell dpkg-buildflags --get CFLAGS 2>/dev/null | $(GREP) -E -o "\-O[0-3]" || echo "-O2")
 # architecture dependent flags for the C and C++ compiler that can be changed by
 # exporting the variable ARCH_FLAGS="..." in the shell, which is used to start build
 ARCH_FLAGS*=
diff --git a/solenv/inc/unxlngi.mk b/solenv/inc/unxlngi.mk
index d86aab1..a10adb6 100644
--- libreoffice-3.5.0/solenv/inc/unxlngi.mk
+++ libreoffice-3.5.0/solenv/inc/unxlngi.mk
@@ -26,7 +26,6 @@
 #*************************************************************************
 
 # mk file for Unix Linux Intel (X86) using GCC, please make generic modifications to unxlng.mk
-CDEFAULTOPT=-Os
 .IF "$(GCCNUMVER)" >= "000400050000"
 ARCH_FLAGS*=-mtune=atom
 .ELSE
diff --git a/solenv/inc/unxlngmips.mk b/solenv/inc/unxlngmips.mk
index 53c927d..c1b7164 100644
--- libreoffice-3.5.0/solenv/inc/unxlngmips.mk
+++ libreoffice-3.5.0/solenv/inc/unxlngmips.mk
@@ -27,6 +27,5 @@
 
 # mk file for Unix Linux Mips using GCC, please make generic modifications to unxlng.mk
 
-CDEFAULTOPT=-Os
 .INCLUDE : unxlng.mk
 CDEFS+=-DMIPS
diff --git a/solenv/inc/unxlngr.mk b/solenv/inc/unxlngr.mk
index 1bcbb66..acd84fb 100644
--- libreoffice-3.5.0/solenv/inc/unxlngr.mk
+++ libreoffice-3.5.0/solenv/inc/unxlngr.mk
@@ -27,7 +27,6 @@
 
 # mk file for Unix Linux ARM using GCC, please make generic modifications to unxlng.mk
 
-CDEFAULTOPT=-Os
 .INCLUDE : unxlng.mk
 CDEFS+=-DARM32
 CFLAGS+=-fno-omit-frame-pointer
diff --git a/solenv/inc/unxlngs.mk b/solenv/inc/unxlngs.mk
index b4e6f5f..f33411a 100644
--- libreoffice-3.5.0/solenv/inc/unxlngs.mk
+++ libreoffice-3.5.0/solenv/inc/unxlngs.mk
@@ -29,7 +29,6 @@
 
 ASM=$(CC)
 AFLAGS=-Wa,-K,PIC -c $(CDEFS)
-CDEFAULTOPT=-Os
 PICSWITCH:=-fPIC
 .INCLUDE : unxlng.mk
 CDEFS+=-DSPARC
